Asked  7 Months ago    Answers:  5   Viewed   30 times

I want to iterate over a list of files. This list is the result of a find command, so I came up with:

getlist() {
  for f in $(find . -iname "foo*")
  do
    echo "File found: $f"
    # do something useful
  done
}

It's fine except if a file has spaces in its name:

$ ls
foo_bar_baz.txt
foo bar baz.txt

$ getlist
File found: foo_bar_baz.txt
File found: foo
File found: bar
File found: baz.txt

What can I do to avoid the split on spaces?

 Answers

17

You could replace the word-based iteration with a line-based one:

find . -iname "foo*" | while read f
do
    # ... loop body
done
Tuesday, June 1, 2021
 
Vlad
answered 7 Months ago
93

Since this happens in dash and dash is simpler, I looked there first.

Seems like exec.c is the place to look, and the relevant functionis are tryexec, which is called from shellexec which is called whenever the shell things a command needs to be executed. And (a simplified version of) the tryexec function is as follows:

STATIC void
tryexec(char *cmd, char **argv, char **envp)
{
        char *const path_bshell = _PATH_BSHELL;

repeat:

        execve(cmd, argv, envp);

        if (cmd != path_bshell && errno == ENOEXEC) {
                *argv-- = cmd;
                *argv = cmd = path_bshell;
                goto repeat;
        }
}

So, it simply always replaces the command to execute with the path to itself (_PATH_BSHELL defaults to "/bin/sh") if ENOEXEC occurs. There's really no magic here.

I find that FreeBSD exhibits identical behavior in bash and in its own sh.

The way bash handles this is similar but much more complicated. If you want to look in to it further I recommend reading bash's execute_command.c and looking specifically at execute_shell_script and then shell_execve. The comments are quite descriptive.

Friday, June 18, 2021
 
conmen
answered 6 Months ago
59

You can try this:

import matplotlib.pyplot as plt

plt.figure()

for n, single_q in enumerate(q_list):
    ax = plt.subplot(len(q_list), 1, n+1)
    GCT_object = parse_gctoo.parse(single_q)
    gene_medians = GCT_object.data_df.median(axis=1)
    plt.hist(gene_medians)
    # tweak title, labels, etc.

plt.show()

Explaining:

  • enumerate iterates over the items (s) while also returning their indices (n);
  • the function subplot(size, column, row) requires these parameters: size is the total amount of subplots in the figure, and row and column determine the position for the current plot. n+1 is necessary to put the plot in the correct position along the plot grid;
  • I edited the rest of the code with your own data
Saturday, August 28, 2021
 
user2722968
answered 4 Months ago
60

Here is a demo that loops through lists of map:

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class mapTest extends ActionSupport {
  public List<Map> listmap;

  public String execute(){
    listmap = new ArrayList();
    Map map = new HashMap();
    map.put("a", "alpha");
    map.put("b", "bravo");
    map.put("c", "charlie");
    listmap.add(map);
    Map map2 = new HashMap();
    map2.put("d", "delta");
    map2.put("e", "echo");
    map2.put("f", "foxtrot");
    listmap.add(map2);
    return SUCCESS;
  }
}

Here is the JSP to render it:

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
        <h1>Map Test</h1>
        <table>
            <thead>
                <tr>
                    <th>List #</th>
                    <th>key</th>
                    <th>value</th>
                </tr>
            </thead>
            <tbody>
                <s:iterator value="listmap" status="stat">
                    <s:iterator>
                        <tr>
                            <th><s:property value="#stat.index"/></th>
                            <td><s:property value="key"/></td>
                            <td><s:property value="value"/></td>
                        </tr>
                    </s:iterator>
                </s:iterator>
            </tbody>
        </table>
    </body>
</html>

Note the inner iterator is context sensitive it will use the last value pushed onto the stack. The status attribute gives us a IteratorStatus object each iteration which is useful if we want to know the current iteration.

Friday, September 10, 2021
 
user542584
answered 3 Months ago
36

You can use this script:

#!/bin/bash
IFS=' ' read -ra arr -p "Enter numbers: "
Enter numbers: 4 -1 2 66 10

sort -n <(printf "%sn" "${arr[@]}")
-1
2
4
10
66
  • IFS=' ' to make read all number delimited by space
  • 'read -ra` to read all numbers in an array
  • sort -n to sort numbers numerically
  • printf "%sn" "${arr[@]}" to print each element of array in separate line
  • <(printf "%sn" "${arr[@]}") is process substitution that make it printf command behave like a file for sort -n command.
Thursday, November 18, 2021
 
Peter Thomas
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share