* [string.sh](http://string.sh)
* name='nana'
* echo My name is $name
* :<<!
* echo 这是多行注释
* echo "ni"
* echo "ni"
* echo "ni"
* !
* [expr.sh](http://expr.sh)
* a=4
* b=7
* #c=$(($b-$a))
* #c=$((b-a))
* #c=`expr $((b-a))`
* #c=$[b-a]
* let c=b-a
* echo $c
* [root@localhost opt]# cat a2.sh
* #!/bin/bash
* a=3
* b=5
* if test $a -eq $b
* then
* echo "a=b"
* else
* echo "a!=b"
* fi
* [root@localhost opt]# cat a3.sh
* #!/bin/bash
* for i in a b c d e f
* do
* echo $i
* done
* [root@localhost opt]# cat a4.sh
* #!/bin/bash
* a=(1 2 3 4 5 6)
* for i in a
* do
* echo ${a[0]}
* echo ${a[1]}
* echo 打印所有数组元素:${a[*]}
* echo a数组长度为:${#a[*]}
* done
* [root@localhost opt]# sh a4.sh
* 1
* 2
* 打印所有数组元素:1 2 3 4 5 6
* a数组长度为:6
* [root@localhost opt]# cat a5.sh
* #!/bin/bash
* a=1
* #注意,while(( )) 中间都是要有空格的,否则报错
* while(( $a<=5 ))
* do
* echo $a
* let "a++"
* done
* [root@localhost opt]# cat a6.sh
* #!/bin/bash
* echo 'Press <CTRL-D> to quit'
* echo -n 'Input a film name:'
* while read FILM
* do
* echo "Yes!$FILM is a good film"
* done
* [root@localhost opt]# cat a7.sh
* #!/bin/bash
* a=0
* until [ ! $a -lt 10 ]
* do
* echo $a
* let a++
* done
* [root@localhost opt]# cat a8.sh
* #!/bin/bash
* echo 'Type 1-4:'
* echo 'Your type is:'
* read num
* case $num in
* 1)echo '1'
* ;;
* 2)echo '2'
* ;;
* 3)echo '3'
* ;;
* 4)echo '4'
* ;;
* *)echo 'None'
* ;;
* esac
* #不用跟Python一样讲究缩进
* [root@localhost opt]# cat a9.sh
* #!/bin/bash
* while :
* do
* echo -n "输入 1 到 5 之间的数字:"
* read aNum
* case $aNum in
* 1|2|3|4|5) echo "你输入的数字为 $aNum!"
* ;;
* *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
* break
* ;;
* esac
* done
* [root@localhost opt]# sh a9.sh
* 输入 1 到 5 之间的数字:4
* 你输入的数字为 4!
* 输入 1 到 5 之间的数字:2
* 你输入的数字为 2!
* 输入 1 到 5 之间的数字:4
* 你输入的数字为 4!
* 输入 1 到 5 之间的数字:9
* 你输入的数字不是 1 到 5 之间的! 游戏结束
* [root@localhost opt]# cat a10.sh
* #!/bin/bash
* while :
* do
* echo -n "输入 1 到 5 之间的数字: "
* read aNum
* case $aNum in
* 1|2|3|4|5) echo "你输入的数字为 $aNum!"
* ;;
* *) echo "你输入的数字不是 1 到 5 之间的!"
* continue
* echo "游戏结束"
* ;;
* esac
* done
* [root@localhost opt]# sh a10.sh
* 输入 1 到 5 之间的数字: 2
* 你输入的数字为 2!
* 输入 1 到 5 之间的数字: 4
* 你输入的数字为 4!
* 输入 1 到 5 之间的数字: 10
* 你输入的数字不是 1 到 5 之间的!
* 输入 1 到 5 之间的数字: 555
* 你输入的数字不是 1 到 5 之间的!
* 输入 1 到 5 之间的数字: ^C
* [cat.sh](http://cat.sh)
* #!/bin/bash
* #将当前路径下所有的.sh格式的文件内容都追加到Test.sh文件中
* echo `cat *.sh > [Test.sh](http://Test.sh)`
* [cut.sh](http://cut.sh)
* # 截图Mysql的临时密码
* grep 'temporary password' /var/log/mysqld.log | cut -d ":" -f 4 | tail -1
* [root@localhost html]# echo $PATH
* /usr/lib/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/admin/bin
* [root@localhost html]# echo $PATH | cut -d ":" -f 2
* /usr/local/bin
* [root@localhost html]# echo $PATH | cut -d ":" -f 2,4
* /usr/local/bin:/usr/bin
* [root@localhost html]# echo $PATH | cut -d ":" -f 2,3,4
* /usr/local/bin:/bin:/usr/bin
* [root@localhost html]# echo $PATH | cut -c 2-5
* usr/
* [root@localhost html]# echo $PATH | cut -c 2-
* usr/lib/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/admin/bin
* [expect.sh](http://expect.sh)
* #!/usr/bin/expect
* # yum install expect -y
* set timeout 30
* spawn mysql -u root -p
* sleep 1
* set temp [exec sh -c {grep 'temporary password' /var/log/mysqld.log | cut -d ":" -f 4 | tail -1 | cut -c 2-}]
* puts "$temp"
* sleep 1
* send "$temp\r"
* interact
* #sql="show databases"
* #$mysql -e "$sql"
* # [调用shell脚本test.sh](http://调用shell脚本test.sh)
* # set testResult [exec sh -c {./[test.sh](http://test.sh)}]
* [ls.sh](http://ls.sh)
* #!/bin/bash
* echo $(ls)
* #!/bin/bash
* [add1.sh](http://add1.sh)
* a=1
* b=2
* c=$((a+b))
* echo $c
* [add2.sh](http://add2.sh)
* a=1
* b=7
* c=`expr $a + $b`
* # 注意:`expr $a + $b`这里面的$a + $b 是有空格的,连在一起的结果是1+7,而不是8
* echo $c
* [add3.sh](http://add3.sh)
* #!/bin/bash
* a=3
* b=4
* let c=a+b
* echo $c
* [head_tail.sh](http://head_tail.sh)
* #!/bin/bash
* echo $(head -5 a1.txt | tail -1)
* [sed_all_cat.sh](http://sed_all_cat.sh)
* #!/bin/bash
* sed -i 's/name/cdtest/g' a2.txt
* echo $(cat a2.txt)
* [sed_line_cat.sh](http://sed_line_cat.sh)
* #!/bin/bash
* sed -i '5s/name/cdtest/g' a2.txt
* echo $(cat a2.txt)
* [grep_head_string_trip.sh](http://grep_head_string_trip.sh)
* #!/bin/bash
* a=$(grep name a1.txt | head -1)
* echo ${a:3:2}
* # 第4和第5个字符
* echo "等待5秒钟"
* sleep 5
* echo "5秒钟已到"
* # sleep : 默认为秒。
* # sleep 1s 表示延迟一秒
* # sleep 1m 表示延迟一分钟
* # sleep 1h 表示延迟一小时
* # sleep 1d 表示延迟一天
* # 等待上1个命令执行完之后,再执行下一个命令
* #!/bin/sh
* command 1
* output=`command 2`
* command 3
* [ou1.sh](http://ou1.sh)
* for i in `seq 1 100`
* do
* if [ $((i%2)) -eq 0 ]
* then
* echo $i
* fi
* done
* [ou2.sh](http://ou2.sh)
* for((i=2;i<=100;i=i+2))
* # i=i+2 等同于 i+=2
* do
* echo $i
* done
* [ou3.sh](http://ou3.sh)
* for i in {2..100..2}
* do
* echo $i
* done
* [sum_ji_sum_ou.sh](http://sum_ji_sum_ou.sh)
* #!/bin/bash
* echo $(ls)
* sum_ji=0
* sum_ou=0
* for i in $(seq 1 100)
* do
* if [ $((i%2)) -eq 1 ]
* # 也可以:if [ $((i%2)) == 1 ]
* # 还可以:if [ $((i%2)) = 1 ]
* then
* ((sum_ji=sum_ji+i))
* # 也可以:((sum_ji+=i))
* else
* ((sum_ou=sum_ou+i))
* fi
* done
* echo "1-100所有奇数之和为: $sum_ji"
* echo "1-100所有偶数之和为:$sum_ou"
* # 也可以:echo 1-100所有偶数之和为:$sum_ou
* [tgz_bz2_zip_file_directory.sh](http://tgz_bz2_zip_file_directory.sh)
* #!/bin/bash
* if [ ! -f "a.tgz" -o ! -f "a.bz2" -o ! -f "a.zip" ];then
* echo "a.tgz a.bz2 a.zip 不存在"
* else
* rm -rf a.tgz a.bz2 a.zip
* fi
* tar -zcvf a.tgz *.txt *.sh
* tar -jcvf a.bz2 *.txt *.sh
* zip a.zip *.txt *.sh
* if [ ! -d "a1" -o ! -d "a2" -o ! -d "a3" ];then
* echo "目录a1 a2 a3不存在"
* else
* rm -rf a1 a2 a3
* fi
* mkdir a1 a2 a3
* tar -zxvf a.tgz -C a1/
* tar -jxvf a.bz2 -C a2/
* unzip -o a.zip -d a3/
* [99.sh](http://99.sh)
* #!/bin/bash
* #echo -n 不换行
* #echo -e 处理特殊字符
* echo "This is the multiple table."
* for((i=1;i<=9;i++))
* do
* for((j=1;j<=i;j++))
* do
* echo -ne "$i*$j=$((i*j))\t"
* done
* echo
* done
* [leap_year_2.sh](http://leap_year_2.sh)
* #! /bin/bash
* echo -n Input year to judge if it is a leap year:
* read year
* let "y1=$year % 4"
* let "y2=$year % 100"
* let "y3=$year % 400"
* if [ "$y1" -eq 0 -a ! "$y2" -eq 0 -o "$y3" -eq 0 ]
* then
* leap=1
* else
* leap=0
* fi
* #if [ "$leap" -eq 1 ]; 这里"$leap"与不加引号一样OK
* if [ $leap -eq 1 ];
* then
* echo "$year is a leap year"
* else
* echo "$year is not a leap year"
* fi
* [root@localhost html]# cat b.sh
* #!/bin/bash
* # 批量导入SQL文件
* p="/tmp" //目录下全是sql文件
* dbUser='root'
* dbPassword='123'
* dbName='zh'
* cd $p;
* for f in `ls $p/*.sql`
* do
* echo $f;
* mysql -u $dbUser -p$dbPassword -f $dbName -e "source $f";
* mv $f $f.done;
* done
* echo 'finished!'
* [root@localhost html]# cat change_pwd.sh
* #!/bin/bash
* #交互修改密码
* set timeout 10
* NPASS="123456a"
* expect << EOF
* spawn passwd
* expect "New password:"
* send "${NPASS}\r"
* expect "Retype new password:"
* send "${NPASS}\r"
* expect eof;
* EOF
* [root@localhost html]# cat mysql_temp_pwd_expect.exp
* #!/usr/bin/expect
* set timeout 30
* spawn mysql -u root -p
* sleep 1
* set temp [exec sh -c {grep 'temporary password' /var/log/mysqld.log | cut -d ":" -f 4 | tail -1 | cut -c 2-}]
* puts "$temp"
* sleep 1
* send "$temp\r"
* interact
* [root@localhost abc]# vi a1.sh
* #!/bin/bash
* #1970-01-01-00:00:00
* start_time=`date +%s`
* echo "Start to run shell script..."
* sleep 5
* for i in `ls /var/www/html`
* do
* echo $i
* done
* sleep 5
* echo "Scripts run over..."
* end_time=`date +%s`
* cost_time=$((end_time-start_time))
* echo "Start time is: $start_time, End time is:$end_time, the totle cost time is:$cost_time seconds!"
* [root@localhost html]# cat test.sh
* #!/bin/bash
* echo -e "\033[0m none \033[0m"
* echo -e "\033[30m black \033[0m"
* echo -e "\033[1;30m dark_gray \033[0m"
* echo -e "\033[0;34m blue \033[0m"
* echo -e "\033[1;34m light_blue \033[0m"
* echo -e "\033[0;32m green \033[0m"
* echo -e "\033[1;32m light_green \033[0m"
* echo -e "\033[0;36m cyan \033[0m"
* echo -e "\033[1;36m light_cyan \033[0m"
* echo -e "\033[0;31m red \033[0m"
* echo -e "\033[1;31m light_red \033[0m"
* echo -e "\033[0;35m purple \033[0m"
* echo -e "\033[1;35m light_purple \033[0m"
* echo -e "\033[0;33m brown \033[0m"
* echo -e "\033[1;33m yellow \033[0m"
* echo -e "\033[0;37m light_gray \033[0m"
* echo -e "\033[1;37m white \033[0m"
* echo -e "\033[0m none \033[0m"
* echo -e "\033[0m none \033[0m"
* echo -e "\033[0m none \033[0m"
* echo -e "\033[0m none \033[0m"
* echo -e "\033[0m none \033[0m"
* echo -e "\033[40;37m 黑底白字 \033[0m"
* echo -e "\033[41;30m 红底黑字 \033[0m"
* echo -e "\033[42;34m 绿底蓝字 \033[0m"
* echo -e "\033[43;34m 黄底蓝字 \033[0m"
* echo -e "\033[44;30m 蓝底黑字 \033[0m"
* echo -e "\033[45;30m 紫底黑字 \033[0m"
* echo -e "\033[46;30m 天蓝底黑字 \033[0m"
* echo -e "\033[47;34m 白底蓝字 \033[0m"
* echo -e "\033[4;31m 下划线红字 \033[0m"
* echo -e "\033[5;31m 红字在闪烁 \033[0m"
* echo -e "\033[8m 消隐 \033[0m "
* [root@localhost html]# vi test.sh
* #!/bin/bash
* LingXing(){
* #read -p "输入菱形半径:" r
* r=2
* for((y=0; y<=2*r; y++))
* do
* for((x=0; x<=2*r; x++))
* do
* #if ((y==-x+r || y==-x+3*r || y==x+r || y==x-r)) 空心
* if ((y>=-x+r && y<=-x+3*r && y<=x+r && y>=x-r)) #实心
* then
* echo -n "*"
* else
* echo -n " "
* fi
* done
* echo ""
* done
* }
* #Function to use in shell like as follows
* LingXing
Shell 简单编程
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- shell编程 Shell 脚本中的数学运算 bash shell 的基础运算主要有4种形式: $(( expre...
- shell编程 if 条件语句 If 条件语句主要有三中形式: if...then…fi, if…then…els...