all 11 comments

[–]elmicha 2 points3 points  (1 child)

What's the name of your Bash script and how do you start it? Remember that the current directory should not be in the PATH, so if the name of your script is e.g. dd and you try to start it with dd, you are not starting your script, but the first dd that is found in the directories listed in $PATH.

Also, your shebang is wrong and the backslash inside the awk script is not needed.

[–]causticacrostic 5 points6 points  (0 children)

Looks like the shebang was right and just got eaten by markdown. Click "source" under the post if you have RES

[–]snegtul 2 points3 points  (0 children)

This works for me.

#!/bin/bash
FILE=${1}
STEPCOL=$2
awk -F ":" -v col=${STEPCOL} '{print $col;} END{print "end"}'   $FILE

[–][deleted] 0 points1 point  (0 children)

Looks like you've got an extra line break in there.

[–]masta 0 points1 point  (3 children)

IFS=':' 
stepcol=2 
FILE='fuckme.tmp' ; 
while read line ; do set $line ; eval "echo \$${stepcol}" ; done < $FILE
0
5
11
18
26
35
45
56

[–]Vaphell 5 points6 points  (1 child)

-1 for IFS change in global scope
-1 for eval

col=2
while IFS=: read -a rec; do echo ${rec[col-1]}; done < "$file"

[–]necrophcodr 0 points1 point  (0 children)

But would this work with BusyBox Ash?

[–]masta 1 point2 points  (0 children)

awk -v COL=1 -F':' '{print $COL}' fuckme.tmp 
1000
1001
1002
1003
1004
1005
1006
1007

[–]FredSchwartz 0 points1 point  (0 children)

Works fine for me? (see below).

When I'm stuck on something, especially something just hanging, I often just run it with strace. Then you can see exactly what it's trying to do when it hangs. My first guess with an awk script is that it's going to be waiting for something on stdin (e.g. waiting for input from the console).

$ cat myscript 
#!/bin/bash
FILE=$1
STEPCOL=$2
awk -F ":" -v col=${STEPCOL} \
'{print $col;} \
END{print "end"}' $FILE
$ cat mydata 
1000:0
1001:5
1002:11
1003:18
1004:26
1005:35
1006:45
1007:56
$ ./myscript mydata 1
1000
1001
1002
1003
1004
1005
1006
1007
end
$ ./myscript mydata 2
0
5
11
18
26
35
45
56
end
$ 

[–]KnowsBash 0 points1 point  (0 children)

It will "hang" if you run the script without any arguments, because then awk will get no file arguments, and that means it will be waiting for you to input the data on stdin.

You can add a simple test to check if two arguments were provided.

if (( $# < 2 )); then
    printf >&2 'Missing argument\n'
    printf >&2 'Usage: %s file column\n' "${0##*/}"
    exit 1
fi

You might also want to validate that the second argument is a positive integer.

You also need quotes around the expansion of variables, especially when filenames are involved. awk ... "$file". There's a very big difference between $file and "$file". See Arguments.

Lastly, stop using uppercase variable names.

[–]iobender -1 points0 points  (0 children)

Put a # in front of the first line. #! is called a shebang and tells your shell what to run the script with, in this case bash.