Linux shell实例精讲学习笔记

来源:互联网 发布:linux有哪些版本及特点 编辑:程序博客网 时间:2024/04/28 20:41

前七章的部分知识在前面的linux shell学习笔记中已经结合进去了。所以笔记从第八章开始。而且这本书的目录编排也是这样,第八章才开始正式涉及shell编程。
有了前面的学习,下面只记载一些在学习的过程中又忘了了一些知识点。所以记录是零散的。

1.
shopt bash2.0以上新的命令,功能和set类似。
给变量赋值时等号两边不可留空格。
环境变量一般使用大写。

2.
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的进程ID号
$@ 传递到脚本的参数列表,并在引号中返回每个参数
$- 显示shell使用的当前选项,与set命令功能相同
$? 显示最后命令的退出状态,0表示没有错误,其他表示有错误

3.
echo 命令中使用" "无法直接识别转义字符,必须使用选项 -e,但可以使用$和`引用变量和命令替换。
$[] 告诉shell对方括号中表达式求值 $[a+b],如$[1+8]

4.
tee
把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中
tee -a file       -----a表示在文件末尾追加,不要-a则覆盖
该命令一般与管道合用

5.
command>filename>&1 ---把标准输出和标准错误重定向
command<<delimiter ---输入直到delimiter分解符
eg:
sqlplus / as sysdba <<EOF
show parameters
exit
EOF

6.命令替换有两种方式,老的korn风格的`linux command`和新的bash风格$(linux command)

7.定义数组
declare -a arry=(a1 a2 a3 ...)
echo ${a[n]}

8.read使用
read -a arryname     ---读入为数组
read -p prompt      ----输出提示,并把提示后的输入存到REPLY内建变量中
[test@szbirdora 1]$ read -p "Enter your name:"
Enter your name:jack
[test@szbirdora 1]$ echo $REPLY
jack

[test@szbirdora 1]$ read -a name
jack tom susan
[test@szbirdora 1]$ echo ${name[2]}
susan
[test@szbirdora 1]$ echo ${name[0]}
jack

9.多进制整型变量表示
declare -i number
number=base#number_in_the_base
如:
declare -i number
number=2#101
number=16#A9

10.条件语句if then fi中if后的表达式可以是条件表达式(布尔表达式)或一组命令,如果是布尔表达式则表达式为真执行then后的语句,如果是命令组,则命令组状态($?)为0执行then后面的语句。但如果是C shell则if后只能跟布尔表达式。



11.here文档和case建立菜单:
eg.
echo "Select a terminal type:"
cat <<EOF
1) linux
2)xterm
3)sun
EOF
read choice
case $choice in
1)TERM=linux
   export TERM
   ;;
2)TERM=xterm
   export TERM
;;
3)TERM=sun
   export TERM
;;
*) echo "please select correct chioce,ths!"
;;
esac
echo "TERM is $TERM"

12.循环比较:
for variable in wordlist;do     ---从文件或列表中给定的值循环
while command;do       ----当command的返回值为0时做循环
until command;do        ----当command的返回值不为0时作循环

select循环菜单
用select建立菜单,需要用PS3来提示用户输入,输入保存在REPLY内建变量中,REPLY的值与内建菜单关联。在构建菜单过程中可以使用COLUMNS和LINES两个变量,COLUMNS决定菜单的列宽度,LINES决定可显示的行数。select和case命令联用可以有效的构造循环菜单。
eg
#!/bin/bash
#Author: hijack
#Usage:Menu
t=0
j=0
d=0
PS3="Please choose one of the three boys or quit:"
select choice in Tom Jack David Quit
do
case $choice in
Tom)
t=$t+1
if (($t==5));then
echo "Tom win"
break
fi
;;
Jack)
j=$j+1
if (($j==5));then
echo "Jack win"
break
fi
;;
David)
d=$d+1
if (($d==5));then
echo "David win"
break
fi
;;
Quit) exit 0;;
*) echo "$REPLY is invalide choice" 1>&2
   echo "Try again!"
;;
esac

done

[test@szbirdora 1]$ sh menu
1) Tom
2) Jack
3) David
4) Quit
Please choose one of the three boys or quit:1
Please choose one of the three boys or quit:2
Please choose one of the three boys or quit:1
Please choose one of the three boys or quit:1
Please choose one of the three boys or quit:1
Please choose one of the three boys or quit:1
Tom win
 12.set 设置位置变量,shift移动位置变量。
set tom jack david -----$1 tom $2 jack $3 david
echo $*
tom jack david
shift 2                     -----移动两个位置,$1,$2删除
echo $*
david
eg:
#!/bin/bash
#Name:shift
#Author : Hijack
#Usage:shift test
#Date:080320
while (($#>0))
do
    echo "$*"
    shift
done

[test@szbirdora 1]$ ./shift 1 2 3 4 5 6 7
1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
4 5 6 7
5 6 7
6 7
7

13。 break n   ---n 代表退出第几层循环,默认退出一层。continue n 类似
#!/bin/bash
#Name   : Mulloop
#Author : Hijack
#Usage : break test
declare -i x=0
declare -i y=0
while true
do
while (( x<20 ))
do
   x=$x+1
   echo $x
   if (( $x==10 ));then
    echo "if"
    break
   fi
done
echo "loop end"
y=$y+1
if (($y>5));then
   break
fi
done

[test@szbirdora 1]$ sh mulloop
1
2
3
4
5
6
7
8
9
10
if
loop end
11
12
13
14
15
16
17
18
19
20
loop end
loop end
loop end
loop end
loop end

#!/bin/bash
#Name   : Mulloop
#Author : Hijack
#Usage : break test
declare -i x=0
declare -i y=0
while true
do
while (( x<20 ))
do
   x=$x+1
   echo $x
   if (( $x==10 ));then
    echo "if"
    break 2
   fi
done
echo "loop end"
y=$y+1
if (($y>5));then
   break
fi
done

[test@szbirdora 1]$ sh mulloop
1
2
3
4
5
6
7
8
9
10
if

14.循环的IO重定向
使用">","|"等重定向符实现循环的IO重定向
如 while ;do
      done >temp$$

    for in
    do
    done |sort
eg
给文件的每行加一个行号,写入文件中
#!/bin/bash
#Name    : loopred
#Author : Hijack
#Usage   : add linenum to the file
#Program : read line to loop from file,add linenum,output to tempfile,mv tempfile to file
declare -i count=0
declare -i total=0
total=`sed -n "$=" $1`
cat $1 | while read line
do
    (($count==0))&& echo -e "Processing file $1...../n"> /dev/tty
    count=$count+1
    echo -e "$count/t$line"
    (($count==$total))&& echo "Process finish,total line number is $count" > /dev/tty
done >temp$$
mv temp$$ $1

[test@szbirdora 1]$ sh loopred testmv
Processing file testmv.....

Process finish,total line number is 19
[test@szbirdora 1]$ vi testmv

1       /u01/test
2       /u01/test/1
3       /u01/test/1/11
4       /u01/test/1/forlist.sh
5       /u01/test/1/optgets.sh
6       /u01/test/1/whiletest.sh
7       /u01/test/1/func.sh
8       /u01/test/1/helloworld.sh
9       /u01/test/1/df.out
10      /u01/test/1/nullfile.txt
11      /u01/test/1/iftest.sh
12      /u01/test/1/myfile
13      /u01/test/1/opt2.sh
14      /u01/test/1/0
15      /u01/test/1/case.sh
16      /u01/test/1/nohup.out
17      /u01/test/1/hellfun.sh
18      /u01/test/1/parm.sh
19      /u01/test/1/test

15。在done后面加&使循环在后台运行,程序继续执行。

16.在函数内可以使用local定义本地变量,local variable。


17.陷阱信号 trap --当一个信号发出传递给进程时,进程进行相关操作,信号包括中断等
     trap ‘command;command’ signal-num                          #trap设置时执行命令
     trap “command;command” signal-num                         #信号到达时执行命令
eg:
[root@linux2 ~]# trap "echo -e 'hello world/n';ls -lh" 2
[root@linux2 ~]# hello world                                            
---ctrl+c

total 100K
-rw-r--r-- 1 root root 1.4K Nov 14 16:53 anaconda-ks.cfg
drwxr-xr-x 2 root root 4.0K Nov 23 13:11 Desktop
-rw-r--r-- 1 root root 53K Nov 14 16:53 install.log
-rw-r--r-- 1 root root 4.9K Nov 14 16:53 install.log.syslog
drwxr-xr-x 2 root root 4.0K Nov 22 13:03 vmware

原创粉丝点击