Linux Shell学习(5)

1. trap (用以捕捉信号)
trap 'action' signal1 signal2 signalN   ##set traps
trap - signal1 signal2 signalN   ##clear traps

2. 定义函数
e.g.
root@localhost :/home/James/mypro/shell/TryOut# test() {
> echo "This is a test function"
> }
root@localhost :/home/James/mypro/shell/TryOut# test
This is a test function


3. Subshell
当运行脚本时,shell会创建一个新进程,叫做subshell,脚本会在这个subshell中执行。
任何用()括起来的命令,都会在subshell中执行。


4. 导出函数
export -f func


5. .(dot) command   和  exec
用.(dot) command执行脚本,那么脚本实际上是在当前shell中执行,而非subshell。
用exec来执行命令或者脚本,那么脚本实际上是在当前shell中执行。
通常,可以用.(dot)命令来设置环境变量和导入常用的函数定义。   . /path/to/my/lib/func.sh

6. compond command (组合命令)
( command1; command2 )
{ command2; command2; }
在()中的命令是在subshell中运行的。
( command1; command2; command3 ) > output.txt会将command{1,2,3}的输出都重定向到output.txt
在{}中的命令会在当前shell中运行。


7. 用declare -f来查看所有定义的函数。
   用unset -f func可以取消函数定义


8. 定义函数
func-name() {
xxx
}

function func-name {
xxx
}

最好用第一种方法,因为它是POSIX兼容的。


9. function的变量
有一点需要特别注意,shell script和c语言、python等语言不通,其函数中的变量,默认是全局变量!!!!(我勒个擦。。。这是在闹哪样啊。。。)
如果要创建局部变量,需要使用local命令. local varName  或者 local var=value


10. funtion的return
return N命令,终止一个function,并且传回return值N。
一个函数是不能返回string类型的值的,但是,我们依然可以通过function来传递这个值。如下:
func() {
....
...
echo XXXX
}

out=$(func)
这时,就把func的输出XXXX存到out变量中。

 

11. Menu Driver Script
=========================================================================
#!/bin/bash                                                                                                                                                            
#A menu driven script sample template                                                                                                                                  

##---------------------------------                                                                                                                                    
# step1: define variables                                                                                                                                              
##---------------------------------                                                                                                                                    
RED='\033[0;41;30m'
STD='\033[0;0;39m'

##---------------------------------                                                                                                                                    
# step2: user defined functions                                                                                                                                        
##---------------------------------                                                                                                                                    
pause() {
    read -p "Press [Enter] Key to continue ... " fackEnterKey
}

func1() {
    echo "func1 called"
    pause
}

func2() {
    echo "func2 called"
    pause
}

#function to display menu                                                                                                                                              
show_menus() {
    clear
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo "  M A I N - M E N U "
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo "1. Call func1"
    echo "2. Call func2"
    echo "3. Exit"
}

#read input from key board and take an action                                                                                                                          
read_options() {
    local choice
    read -p "Enter choice [1 - 3] " choice
    case $choice in
        1) func1 ;;
        2) func2 ;;
        3) exit 0 ;;
        *) echo -e "${RED}Error...${STD}" && sleep 1
    esac
}


##---------------------------------                                                                                                                                    
# step3: trap Ctrl-c, Ctrl-z, and quit signals                                                                                                                         
##---------------------------------                                                                                                                                    
trap '' SIGINT SIGSTP SIGQUIT


##---------------------------------                                                                                                                                    
# step4: main logic -- infinite loop                                                                                                                                   
##---------------------------------                                                                                                                                    
while true; do
    show_menus
    read_options
done

============================================================================================


12. Dialog Boxes
判断dialog是否已经安装
ret=$(which dialog); if [ -z $ret ]; then echo "dialog not installed"; fi

安装dialog
apt-get install dialog

"Hello World!"示例
dialog --title "Hello" --msgbox 'Hello World!' 6 20

只要终端支持控制符,它就支持dialog!Good!(好高的移植性啊!)


 

 

你可能感兴趣的:(linux,shell,function,dialog,trap)