Posted in Shell Scripting on February 14th, 2008 No Comments »
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]
Posted in Shell Scripting, Unix on October 31st, 2007 No Comments »
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
Posted in Shell Scripting on October 15th, 2007 No Comments »
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
Posted in Shell Scripting on July 11th, 2007 1 Comment »
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
Posted in Shell Scripting on May 1st, 2007 1 Comment »
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
Posted in Shell Scripting on February 12th, 2007 No Comments »
file existence can be tested for in a conditional using the -a operator:
if [ -a filename.txt ]
then
echo “file here!”
fi
Posted in Shell Scripting on February 12th, 2007 10 Comments »
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”
Posted in Shell Scripting on February 1st, 2007 No Comments »
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”` […]
Posted in Shell Scripting on January 31st, 2007 5 Comments »
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.
Posted in Shell Scripting on January 29th, 2007 3 Comments »
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 […]