Feed on
Posts
Comments

Archive for the 'Shell Scripting' Category

to  send the standard output and standard error streams from your shell script somewhere, place the following at the top of your script:
#!/bin/bash
exec &>  output.file;
[the rest of your code]

to measure how much time a program takes to execute from the command-line, use the ‘time’ function.
for instance, to time the ‘date’ command:
>> time date
Wed Oct 31 14:12:41 EDT 2007
real    0m0.003s
user    0m0.000s
sys     0m0.002s

to count the number of times you match a substring in a larger text file, you can use the following shell one-liner:
>> sed “s/[sub_str]/[sub_str]\n/g” [text_file] | wc -l

to spread a one-line command over multiple lines in a shell script, end each line with a slash and indent the next line.  for instance:
cat $1 | sed ’s/>FALF/F_FAL/g’  \
| sed ’s/>FAL1/1_FAL/g’  \
| sed ’s/>FAL5/5_FAL/g’  \
| sed ’s/>FALZ/Z_FAL/g’  \
| sed ’s/>OUTGROUP/5_OUTGROUP/’ > $2

turns out floating point division (or any other type of floating point math) isn’t built-into the bash shell.  instead, you’ll have to use the bc utility:
echo “6.0/10″ | bc -l

file existence can be tested for in a conditional using the -a operator:
if [ -a filename.txt ]
then
echo “file here!”
fi

let’s say you’ve got:
$j=”foo”
and you’d like to tack on its good partner ‘bar’. then, use the dollar sign and curly braces to delineate variable from string:
$k=”${j}bar”

say you’ve got a stream of filenames that you’d like to remove.  for instance, i’ve got a bunch of filenames that pop out of this command:
grep -v “,” `search “boot0.txt”` | sed ’s/boot.*//’
turns out i can’t directly pipe this to rm.  instead, i’ve got to use the xargs command, as so:
grep -v “,” `search “boot0.txt”` […]

to let the variable ‘j’ equal the output of a shell command, such as find, try:
j=$(find `echo $i | sed ’s/AnGST\/.*/AnGST\//’` -name “*.seq”)
basically, it looks like all you need to do is enclose the command with a dollar-sign and some parentheses.  now, your shell scripts won’t need to be populated by unending lines of pipes.

i’ve just discovered sed.  this thing is wonderful; it’s like an in-line perl on the command-line.  now, it looks like i can use the shell to do things that even a perl program would be overkill for.
for instance, i’ve got these files:
[ldavid@subtilis data]$ ls
ECprot119.phy   ECprot1519.phy  ECprot19.phy    ECprot2519.phy
ECprot1219.phy  ECprot1619.phy  ECprot2019.phy  ECprot3119.phy
ECprot1319.phy  ECprot1819.phy  ECprot219.phy   ECprot519.phy
i want to […]

Next »

More blogs about http://desk.stinkpot.org:8080/tricks.