Shell学习笔记(持续更新)

  1. 使用here文档,模拟用户输入

 here文档形式:

commond <<LABEL
......
LABEL
#使用bc计算百分比
PRECENT_USED=$(bc <<EOF
scale=4
($SW_USED / $SW_TOTAL) * 100
EOF
)
#使用expect交互命令
/usr/bin/expect  << EOF
spawn ssh 192.168.1.100
expect {
	"password:" {
		send "123456\r"
		expect "#"         {send "exit\r"}
		expect "#"
		}
	eof
}
EOF

2. 设置变量类型

typeset -u VARIABLE #大写
typeset -l VARIABLE #小写
typeset -i16 VARIABLE #16进制
typeset -i8 VARIABLE #8进制

3. 高亮显示

#高亮替换
sed s/current_string/$(tput smso)new_string$(tput rmso)/g
#! /usr/bin/ksh
#高亮菜单显示
THIS_HOST=$(hostname)
OPT=" "
MSG="           "
while [[ $OPT != 99 ]]
do
        clear
        tput smso
        echo "                  $THIS_HOST\c"
        echo "                  "
        tput rmso
        echo "\n"

        echo "$(tput smso)1$(tput rmso) - Tape Management"
        echo "$(tput smso)2$(tput rmso) - Tape Management2"
        echo "\n\n"
        echo "$(tput smso)99$(tput rmso) - Logout\n"

        tput smso
        echo "                  ${MSG}\c"
        echo "                  "
        tput rmso
    
        echo "Selection: \c"
        read OPT 
    
        MSG="Invalid option selected."

        case $OPT in
        1)  
                MSG="you select 1"
                ;;  
        2)  
                MSG="you select 2"
                ;;  
        esac
done
clear

4. 设置trap陷阱

trap 'echo "\nExiting...";exit' 1 2 3 15

5. select菜单

#!/usr/bin/ksh

PS3="Is today your birthday?"

echo "\n"

select choose in Yes No Quit
do
        case $choose in
        Yes)
                echo "yes choose\n"
                ;;  
        No) 
                echo "no choose\n"
                ;;  
        Quit)
                echo "quit choose\n"
                break
                ;;  
        *)  
                echo "choose again\n"
                ;;  
        esac
done

猜你喜欢

转载自my.oschina.net/u/945874/blog/1476121