fname()
{
echo $1,$2;
echo "$@";
echo "$*";
return0;
}
$@比$*用的多,因为$@将所有的参数作为一个单个字符串。$*将每个参数作为单个实体。
递归函数
F()
{
echo $1;
F hello;
sleep 1;
}
当我们结合多个命令时,我们通常使用stdin到给定输入,或者stdout到给定输出。在这些的上下文中,命令被称之为filters。
While we combine multiple commands, we usually use stdin to give input and stdout to provide an output.In this context, the commands are called filters. We connect each filter using pipes, the piping operator being |. An example isas follows:
$ cmd1 | cmd2 | cmd3
Here we combine three commands. The output of cmd1 goes to cmd2 and output of cmd2 goes to cmd3 andthe final output (which comes out of cmd3) will be printed, orit can be directed to a file
cat
-n number all output lines【所有输出行】
[root@server4 ~]# pwd
/root
[root@server4 ~]# (cd /bin; ls);
[ gvfs-open python2
a2p gvfs-rename python2.7
abrt-action-analyze-backtrace gvfs-rm qemu-ga
abrt-action-analyze-c gvfs-save qemu-img
····
gvfs-monitor-file pygtk-demo znew
gvfs-mount pyinotify zsoelim
gvfs-move python
[root@server4 ~]# pwd
/root
pwd;
(cd /bin;ls)
pwd;
命令在子shell中的执行,不影响当前shell。可以使用一个()操作符去定义子shell。
Most ofthe input libraries in any programming language readthe input fromthe keyboard; butstring input termination is done when returnis pressed.
[root@server4 ~]# read -p "Enter input:" -s var
Enter input:[root@server4 ~]# echo $var
fsdodf
[root@server4 ~]#
还是不能理解这个结束循环的功能:
repeat() {whiletrue;do $@ && return; done}
repeat() {while :; do $@ && return; done }
repeat() {while :; do $@ && return; sleep 30;done}
internal field separator(IFS)
crontab重定向命令到终端。
date > /dev/tty1 #date输出重定向到需要的终端
-e支持转义
[root@server4 ~]# echo -e \a
a
data="name,sex,rollno,location"
oldIFS=$IFS #backUp
IFS=, #now, the internal field separator(IFS) is a commaforitemin $data;do; echo Item:$item;done
IFS=$oldIFS #reset
[root@server4 ~]# for item in $data; do echo Item:$item;done
Item:name
Item:sex
Item:rollno
Item:location
这个for循环,会按照分隔符自动将data分割。
[root@server4 ~]#
The default value of IFS is a space component (newline, tab, or a spacecharacter).
[ condition ] && action; # action executesifthe condition istrue
[ condition ] || action; # action executesifthe condition isfalse
&& isthe logical AND operation and || isthe logical OR
operation. This is a very helpful trick while writing Bash scripts
[ $var1 -ne 0 -a $var2 -gt 2 ] # using and -a
变量var1不等于0,并且变量var2大于2
[ $var1 -ne 0 -o var2 -gt 2 ] # OR -o
变量var1不等于0,或者var2大于2
Note that a spaceis provided afterandbefore =, ifitisnot provided, itisnot a comparison, butit becomes an assignment statement.