A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts

How to really loop over filenames with spaces in

 Don't parse the output of "ls".

Instead:

for file in *

do

  ...

done


(source)

Ctrl-C doesn't interrupt loop in Bash script

If you want Ctrl-C to be able to stop your loop in Bash, put this inside the loop:

trap "echo Exited!; exit;" SIGINT SIGTERM

(source)

For-each over static values in XSL

When you want to do this:

<xsl:for-each select="$some_value in ('a','b','c','d')"><!-- invalid? -->

You have to do this:

<xsl:variable name="items">
    <item>a</item>
    <item>b</item>
    <item>c</item>
    <item>d</item>
</xsl:variable>

<xsl:for-each select="$items/item">
    <xsl:choose>
    <xsl:when test="$some_value = .">

Bash: How to loop through a list of terms containing spaces

Change the Internal Field Separator (IFS):

O=$IFS
IFS=$(echo -en "\n\b")
for f in $(ls *)
do
echo "$f"
done
IFS=$O

Pass in an extra parameter to perl -n

Use an environment variable

for k in $(cat list_of_ids); do export KK=$k; perl -F"\t" -lane'print if ($F[3] eq $ENV{KK})' data/master_list_of_queries >> new_data_file; done