Shell脚本攻略读书笔记之五

Shell脚本攻略读书笔记之五

fname()
{
    echo $1,$2;
    echo "$@";
    echo "$*";
    return 0;
}
$@比$*用的多,因为$@将所有的参数作为一个单个字符串。$*将每个参数作为单个实体。

递归函数
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 is as follows:
$ cmd1 | cmd2 | cmd3
Here we combine three commands. The output of cmd1 goes to cmd2 and output of cmd2 goes to cmd3 and the final output (which comes out of cmd3) will be printed, or it 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 of the input libraries in any programming language read the input from the keyboard; but string input termination is done when return is pressed.




[root@server4 ~]# read -p "Enter input:" -s var
Enter input:[root@server4 ~]# echo $var
fsdodf
[root@server4 ~]# 

还是不能理解这个结束循环的功能:
repeat() {while true;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 comma
for item in $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 space character).


[ condition ] && action; # action executes
if the condition is true

[ condition ] || action; # action executes
if the condition is false
&& is the logical AND operation and || is the 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 space is provided after and before =, if it is not provided, it is not a comparison, but it becomes an assignment statement.

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/81414700
今日推荐