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*")
    echo "File found: $f"
    # do something useful

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

$ ls
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?



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

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

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:

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


        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
answered 6 Months ago

You can try this:

import matplotlib.pyplot as plt


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)
    # tweak title, labels, etc.


  • 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
answered 4 Months ago

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");
    Map map2 = new HashMap();
    map2.put("d", "delta");
    map2.put("e", "echo");
    map2.put("f", "foxtrot");
    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"
        <h1>Map Test</h1>
                    <th>List #</th>
                <s:iterator value="listmap" status="stat">
                            <th><s:property value="#stat.index"/></th>
                            <td><s:property value="key"/></td>
                            <td><s:property value="value"/></td>

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
answered 3 Months ago

You can use this script:

IFS=' ' read -ra arr -p "Enter numbers: "
Enter numbers: 4 -1 2 66 10

sort -n <(printf "%sn" "${arr[@]}")
  • 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 :