# 指定对话框左上角在屏幕的上的做坐标
--timeout # 超时,返回的错误代码255,如果用户在指定的时间内没有给出相应动作,就按超时处理
--defaultno # 使选择默认为no
--default-item # 设置在一份清单,表格或菜单中的默认项目。通常在框中的第一项是默认
--sleep 5 # 在处理完一个对话框后静止(延迟)的时间(秒)
--max-input size # 限制输入的字符串在给定的大小之内。如果没有指定,默认是2048
--keep-window # 退出时不清屏和重绘窗口。当几个组件在同一个程序中运行时,对于保留窗口内容很有用的
}
dialog --title "Check me" --checklist "Pick Numbers" 15 25 3 1 "one" "off" 2 "two" "on" # 多选界面[方括号]
dialog --title "title" --radiolist "checklist" 20 60 14 tag1 "item1" on tag2 "item2" off # 多选界面(圆括号)
dialog --title "title" --menu "MENU" 20 60 14 tag1 "item1" tag2 "item2" # 单选界面
dialog --title "Installation" --backtitle "Star Linux" --gauge "Linux Kernel" 10 60 50 # 进度条
dialog --title "标题" --backtitle "Dialog" --yesno "说明" 20 60 # 选择yes/no
dialog --title "公告标题" --backtitle "Dialog" --msgbox "内容" 20 60 # 公告
dialog --title "hey" --backtitle "Dialog" --infobox "Is everything okay?" 10 60 # 显示讯息后立即离开
dialog --title "hey" --backtitle "Dialog" --inputbox "Is okay?" 10 60 "yes" # 输入对话框
dialog --title "Array 30" --backtitle "All " --textbox /root/txt 20 75 # 显示文档内容
dialog --title "Add" --form "input" 12 40 4 "user" 1 1 "" 1 15 15 0 "name" 2 1 "" 2 15 15 0 # 多条输入对话框
dialog --title "Password" --insecure --passwordbox "请输入密码" 10 35 # 星号显示输入--insecure
dialog --stdout --title "日历" --calendar "请选择" 0 0 9 1 2010 # 选择日期
dialog --title "title" --menu "MENU" 20 60 14 tag1 "item1" tag2 "item2" 2>tmp # 取到结果放到文件中(以标准错误输出结果)
a=`dialog --title "title" --stdout --menu "MENU" 20 60 14 tag1 "item1" tag2 "item2"` # 选择操作赋给变量(使用标准输出)
dialog菜单实例{
while :
do
clear
menu=`dialog --title "title" --stdout --menu "MENU" 20 60 14 1 system 2 custom`
[ $? -eq 0 ] && echo "$menu" || exit # 判断dialog执行,取消退出
while :
do
case $menu in
1)
list="1a "item1" 2a "item2"" # 定义菜单列表变量
;;
2)
list="1b "item3" 2b "item4""
;;
esac
result=`dialog --title "title" --stdout --menu "MENU" 20 60 14 $list`
[ $? -eq 0 ] && echo "$result" || break # 判断dialog执行,取消返回菜单,注意:配合上层菜单循环
read
done
done
}
}
select菜单{
# 输入项不在菜单自动会提示重新输入
select menuitem in pick1 pick2 pick3 退出
do
echo $menuitem
case $menuitem in
退出)
exit
;;
*)
select area in area1 area2 area3 返回
do
echo $area
case area in
返回)
break
;;
*)
echo "对$area操作"
;;
esac
done
;;
esac
done
}
shift{
./cs.sh 1 2 3
#!/bin/sh
until [ $# -eq 0 ]
do
echo "第一个参数为: $1 参数个数为: $#"
#shift 命令执行前变量 $1 的值在shift命令执行后不可用
shift
done
}
getopts给脚本加参数{
#!/bin/sh
while getopts :ab: name
do
case $name in
a)
aflag=1
;;
b)
bflag=1
bval=$OPTARG
;;
\?)
echo "USAGE:`basename $0` [-a] [-b value]"
exit 1
;;
esac
done
if [ ! -z $aflag ] ; then
echo "option -a specified"
echo "$aflag"
echo "$OPTIND"
fi
if [ ! -z $bflag ] ; then
echo "option -b specified"
echo "$bflag"
echo "$bval"
echo "$OPTIND"
fi
echo "here $OPTIND"
shift $(($OPTIND -1))
echo "$OPTIND"
echo " `shift $(($OPTIND -1))` "
}
tclsh{
set foo "a bc" # 定义变量
set b {$a}; # 转义 b的值为" $a " ,而不是变量结果
set a 3; incr a 3; # 数字的自增. 将a加3,如果要减3,则为 incr a –3;
set c [expr 20/5]; # 计算 c的值为4
puts $foo; # 打印变量
set qian(123) f; # 定义数组
set qian(1,1,1) fs; # 多维数组
parray qian; # 打印数组的所有信息
string length $qian; # 将返回变量qian的长度
string option string1 string2; # 字符相关串操作
# option 的操作选项:
# compare 按照字典的排序方式进行比较。根据string1 string2分别返回-1,0,1
# first 返回string2中第一次出现string1的位置,如果没有出现string1则返回-1
# last 和first相反
# trim 从string1中删除开头和结尾的出现在string2中的字符
# tolower 返回string1中的所有字符被转换为小写字符后的新字符串
# toupper 返回string1中的所有字符串转换为大写后的字符串
# length 返回string1的长度
set a 1;while {$a < 3} { set a [incr a 1;]; };puts $a # 判断变量a小于3既循环
for {initialization} {condition} {increment} {body} # 初始化变量,条件,增量,具体操作
for {set i 0} {$i < 10} {incr i} {puts $i;} # 将打印出0到9
if { 表达式 } {
#运算;
} else {
#其他运算;
}
switch $x {
字符串1 { 操作1 ;}
字符串2 { 操作2 ;}
}
foreach element {0 m n b v} {
# 将在一组变元中进行循环,并且每次都将执行他的循环体
switch $element {
# 判断element的值
}
}
expect交互{
exp_continue # 多个spawn命令时并行
interact # 执行完成后保持交互状态,把控制权交给控制台
expect "password:" # 判断关键字符
send "passwd\r" # 执行交互动作,与手工输入密码的动作等效。字符串结尾加"\r"
ssh后sudo{
#!/bin/bash
#sudo注释下行允许后台运行
#Defaults requiretty
#sudo去掉!允许远程
#Defaults !visiblepw
/usr/bin/expect -c '
set timeout 5
spawn ssh -o StrictHostKeyChecking=no xuesong1@192.168.42.128 "sudo grep xuesong1 /etc/passwd"
expect {
"passphrase" {
send_user "sshkey\n"
send "xuesong\r";
expect {
"sudo" {
send_user "sudo\n"
send "xuesong\r"
interact
}
eof {
send_user "sudo eof\n"
}
}
}
"password:" {
send_user "ssh\n"
send "xuesong\r";
expect {
"sudo" {
send_user "sudo\n"
send "xuesong\r"
interact
}
eof {
send_user "sudo eof\n"
}
}
}
"sudo" {
send_user "sudo\n"
send "xuesong\r"
interact
}
eof {
send_user "ssh eof\n"
}
}
'
}
ssh执行命令操作{
/usr/bin/expect -c "
proc jiaohu {} {
send_user expect_start
expect {
password {
send ${RemotePasswd}\r;
send_user expect_eof
expect {
\"does not exist\" {
send_user expect_failure
exit 10
}
password {
send_user expect_failure
exit 5
}
Password {
send ${RemoteRootPasswd}\r;
send_user expect_eof
expect {
incorrect {
send_user expect_failure
exit 6
}
eof
}
}
eof
}
}
passphrase {
send ${KeyPasswd}\r;
send_user expect_eof
expect {
\"does not exist\" {
send_user expect_failure
exit 10
}
passphrase{
send_user expect_failure
exit 7
}
Password {
send ${RemoteRootPasswd}\r;
send_user expect_eof
expect {
incorrect {
send_user expect_failure
exit 6
}
eof
}
}
eof
}
}
Password {
send ${RemoteRootPasswd}\r;
send_user expect_eof
expect {
incorrect {
send_user expect_failure
exit 6
}
eof
}
}
\"No route to host\" {
send_user expect_failure
exit 4
}
\"Invalid argument\" {
send_user expect_failure
exit 8
}
\"Connection refused\" {
send_user expect_failure
exit 9
}
\"does not exist\" {
send_user expect_failure
exit 10
}
\"Connection timed out\" {
send_user expect_failure
exit 11
}
timeout {
send_user expect_failure
exit 3
}
eof
}
}
set timeout $TimeOut
switch $1 {
Ssh_Cmd {
spawn ssh -t -p $Port -o StrictHostKeyChecking=no $RemoteUser@$Ip /bin/su - root -c \\\"$Cmd\\\"
jiaohu
}
Ssh_Script {
spawn scp -P $Port -o StrictHostKeyChecking=no $ScriptPath $RemoteUser@$Ip:/tmp/${ScriptPath##*/};
jiaohu
spawn ssh -t -p $Port -o StrictHostKeyChecking=no $RemoteUser@$Ip /bin/su - root -c \\\"/bin/sh /tmp/${ScriptPath##*/}\\\" ;
jiaohu
}
Scp_File {
spawn scp -P $Port -o StrictHostKeyChecking=no -r $ScpPath $RemoteUser@$Ip:${ScpRemotePath};
jiaohu
}
}
"
}
交互双引号引用较长变量{
#!/bin/bash
RemoteUser=xuesong12
Ip=192.168.1.2
RemotePasswd=xuesong
Cmd="/bin/echo "$PubKey" > "$RemoteKey"/authorized_keys"
/usr/bin/expect -c "
set timeout 10
spawn ssh -o StrictHostKeyChecking=no $RemoteUser@$Ip {$Cmd};
expect {
password: {
send_user RemotePasswd\n
send ${RemotePasswd}\r;
interact;
}
eof {
send_user eof\n
}
}
"
}
telnet{
#!/bin/bash
Ip="10.0.1.53"
a="\{\'method\'\:\'doLogin\'\,\'params\'\:\{\'uName\'\:\'bobbietest\'\}"
/usr/bin/expect -c"
set timeout 15
spawn telnet ${Ip} 8000
expect "Escape"
send "${a}\\r"
expect {
-re "\"err.*none\"" {
exit 0
}
timeout {
exit 1
}
eof {
exit 2
}
}
"
echo $?
}
}
}
}
9实例{
从1叠加到100{
echo $[$(echo +{1..100})]
echo $[(100+1)*(100/2)]
seq -s '+' 100 |bc
}
判断参数是否为空-空退出并打印null{
#!/bin/sh
echo $1
name=${1:?"null"}
echo $name
}
循环数组{
for ((i=0;i /dev/null 2>&1
then
if echo $a | grep -e '^[0-9]\{4\}-[01][0-9]-[0-3][0-9]$'
then
break
else
echo "您输入的日期不合法,请从新输入!"
fi
else
echo "您输入的日期不合法,请从新输入!"
fi
done
echo "日期为$a"
}
打印日期段所有日期{
#!/bin/bash
qsrq=20010101
jsrq=20010227
n=0
>tmp
while :;do
current=$(date +%Y%m%d -d"$n day $qsrq")
if [[ $current == $jsrq ]];then
echo $current >>tmp;break
else
echo $current >>tmp
((n++))
fi
done
rq=`awk 'NR==1{print}' tmp`
}
打印提示{
cat
then
if echo "$line" | grep -v "#include "
then
sed -i ''$i'i\\/\/All header files are include' incl
i=`expr $i + 1`
fi
fi
lastrow="$line"
done
}
查询数据库其它引擎{
#/bin/bash
path1=/data/mysql/data/
dbpasswd=db123
#MyISAM或InnoDB
engine=InnoDB
if [ -d $path1 ];then
dir=`ls -p $path1 |awk '/\/$/'|awk -F'/' '{print $1}'`
for db in $dir
do
number=`mysql -uroot -p$dbpasswd -A -S "$path1"mysql.sock -e "use ${db};show table status;" |grep -c $engine`
if [ $number -ne 0 ];then
echo "${db}"
fi
done
fi
}
批量修改数据库引擎{
#/bin/bash
for db in test test1 test3
do
tables=`mysql -uroot -pdb123 -A -S /data/mysql/data/mysql.sock -e "use $db;show tables;" |awk 'NR != 1{print}'`
for table in $tables
do
mysql -uroot -pdb123 -A -S /data/mysql/data/mysql.sock -e "use $db;alter table $table engine=MyISAM;"
done
done
}
将shell取到的数据插入mysql数据库{
mysql -u$username -p$passwd -h$dbhost -P$dbport -A -e "
use $dbname;
insert into data values ('','$ip','$date','$time','$data')
"
}
两日期间隔天数{
D1=`date -d '20070409' +"%s"`
D2=`date -d '20070304 ' +"%s"`
D3=$(($D1 - $D2))
echo $(($D3/60/60/24))
}
while执行ssh只循环一次{
cat - # 让cat读连接文件stdin的信息
seq 10 | while read line; do ssh localhost "cat -"; done # 显示的9次是被ssh吃掉的
seq 10 | while read line; do ssh -n localhost "cat -"; done # ssh加上-n参数可避免只循环一次
}
ssh批量执行命令{
#版本1
#!/bin/bash
while read line
do
Ip=`echo $line|awk '{print $1}'`
Passwd=`echo $line|awk '{print $2}'`
ssh -n localhost "cat -"
sshpass -p "$Passwd" ssh -n -t -o StrictHostKeyChecking=no root@$Ip "id"
done$tmpfile # 创建文件标示4,以读写方式操作管道$tmpfile
rm $tmpfile # 将创建的管道文件清除
thred=4 # 指定并发个数
seq=(1 2 3 4 5 6 7 8 9 21 22 23 24 25 31 32 33 34 35) # 创建任务列表
# 为并发线程创建相应个数的占位
{
for (( i = 1;i<=${thred};i++ ))
do
echo; # 因为read命令一次读取一行,一个echo默认输出一个换行符,所以为每个线程输出一个占位换行
done
} >&4 # 将占位信息写入管道
for id in ${seq} # 从任务列表 seq 中按次序获取每一个任务
do
read # 读取一行,即fd4中的一个占位符
(./ur_command ${id};echo >&4 ) & # 在后台执行任务ur_command 并将任务 ${id} 赋给当前任务;任务执行完后在fd4种写入一个占位符
done &- # 关闭管道
}
shell并发函数{
function ConCurrentCmd()
{
#进程数
Thread=30
#列表文件
CurFileName=iplist.txt
#定义fifo文件
FifoFile="$$.fifo"
#新建一个fifo类型的文件
mkfifo $FifoFile
#将fd6与此fifo类型文件以读写的方式连接起来
exec 6<>$FifoFile
rm $FifoFile
#事实上就是在文件描述符6中放置了$Thread个回车符
for ((i=0;i<=$Thread;i++));do echo;done >&6
#此后标准输入将来自fd5
exec 5&6
#当进程结束以后,再向fd6中追加一个回车符,即补上了read -u6减去的那个
} &
done
#等待所有后台子进程结束
wait
#关闭df6
exec 6>&-
#关闭df5
exec 5>&-
}
}
函数{
ip(){
echo "a 1"|awk '$1=="'"$1"'"{print $2}'
}
web=a
ip $web
}
检测软件包是否存在{
rpm -q dialog >/dev/null
if [ "$?" -ge 1 ];then
echo "install dialog,Please wait..."
yum -y install dialog
rpm -q dialog >/dev/null
[ $? -ge 1 ] && echo "dialog installation failure,exit" && exit
echo "dialog done"
read
fi
}
游戏维护菜单-修改配置文件{
#!/bin/bash
conf=serverlist.xml
AreaList=`awk -F '"' '//dev/null 2>&1
then
complaint='申诉成功'
elif echo $results | grep '该IP的脱离申请已被他人提交' > /dev/null 2>&1
then
complaint='申诉重复'
elif echo $results | grep '申请由于近期内有被拒绝的记录' > /dev/null 2>&1
then
complaint='申诉拒绝'
else
complaint='异常'
fi
else
IpStatus='正常'
complaint='无需申诉'
fi
echo "$Ip $IpStatus $complaint" >> $(date +%Y%m%d_%H%M%S).log
done
}
Web Server in Awk{
#gawk -f file
BEGIN {
x = 1 # script exits if x < 1
port = 8080 # port number
host = "/inet/tcp/" port "/0/0" # host string
url = "http://localhost:" port # server url
status = 200 # 200 == OK
reason = "OK" # server response
RS = ORS = "\r\n" # header line terminators
doc = Setup() # html document
len = length(doc) + length(ORS) # length of document
while (x) {
if ($1 == "GET") RunApp(substr($2, 2))
if (! x) break
print "HTTP/1.0", status, reason |& host
print "Connection: Close" |& host
print "Pragma: no-cache" |& host
print "Content-length:", len |& host
print ORS doc |& host
close(host) # close client connection
host |& getline # wait for new client request
}
# server terminated...
doc = Bye()
len = length(doc) + length(ORS)
print "HTTP/1.0", status, reason |& host
print "Connection: Close" |& host
print "Pragma: no-cache" |& host
print "Content-length:", len |& host
print ORS doc |& host
close(host)
}
function Setup() {
tmp = "\
Simple gawk server\
\
terminate script\
\
"
return tmp
}
function Bye() {
tmp = "\Simple gawk server\
Script Terminated...\
"
return tmp
}
function RunApp(app) {
if (app == "xterm") {system("xterm&"); return}
if (app == "xcalc" ) {system("xcalc&"); return}
if (app == "xload" ) {system("xload&"); return}
if (app == "exit") {x = 0}
}
}
}