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

 

转载于:https://my.oschina.net/u/945874/blog/1476121

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值