Shell study notes (continuously updated)

  1. Using the documentation here, simulate user input

 here document form:

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. Set the variable type

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

3. Highlight

#高亮替换
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. Set up traps

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

5. select menu

#!/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

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326807097&siteId=291194637