Personal tools
You are here: Home log shell
Document Actions

shell

1   arguments of shell function(http://www.informit.com/articles/article.asp?p=29301)

arguments are available as $1, $2, and so on, whereas the set of all the arguments is available as $@.:

printMsg () { echo "$1: $2" ; }
execute it as follows::
printMsg Error Failed

2   awk

2.1   2007-05-11 how to distinguish $1 in shell and awk

$1 in shell is position variable, while in awk it's field variable.

use prime. If awk's command is wrapped by prime, wrapping $1 with prime turns it into shell position variable:

func() {
awk 'NR=='$2' {print $'$1'}' $3
}

(use it as)
func 6 400 /etc/password

use double-quote and anti-slash. In double-quoted commandline, $1 means shell position variable while $1 means awk field variable:

awk "{if (\$4>$cutoff) print \$2,$i,\$3}" data.file

(2006-08-20) When awk is used in command substitution, only the first approach works. The shell variable could also be used similar as the position variable. But it only works when the shell variable's value is number, otherwise, it displays nothing.:

var=`awk 'NR=='$shell_var' {print $1}' data.file`

2.2   2009-6-19 select certain lines of a file

For example, select all lines which is not at line 149 and 108:

awk 'NR!=149 && NR!=108 {print}' /tmp/call_method_42_uniq_1st_3_column.tsv

2.3   2009-7-30 change the suffix of files

For example, change all *.cc to *.c:

for i in `ls *.cc`; do name=`echo $i| awk 'BEGIN {FS="."} {print $1}'`; echo $name;  mv $name.cc $name.c ; done

3   2007-05-11 merge stdout and stderr in pipe

append & after |, >, >> would merge them together:

cat fdsa .procmailrc >&/tmp/std

4   2007-07-19 sequential for loop

(http://www.freeos.com/guides/lsst/ is good tutorial for shell programming):

for ((  i = 0 ;  i <= 5;  i++  ))
do
 echo "Welcome $i times"
done

5   2008-04-03 how to do calculation in shell

5.1   bc

bc is an arbitrary precision calculator language

without any argument, it opens a interactive environment. Otherwise, the argument is treated as an input filename. Use pipe to let bc read from stdin

echo 3 + 1|bc

5.2   python

similar strategy. use pipe to redirect input to stdin.:

echo 'print 3+1'|python

6   2008-10-29 shell in at doesn't support c-style for loop

c-style shell for loop:

for((i=0;i<10;i++)); do echo $i; done

Seems this is a bash invention. Putting bash in front of other commands in at doesn't help. So changing the link /bin/sh to /bin/bash works.

Related content
« November 2009 »
Su Mo Tu We Th Fr Sa
1234567
891011121314
15161718192021
22232425262728
2930
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: