实例1
写一个脚本:如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型;(不要怀疑,就是这么简单)
方法1
[root@localhost 20150921]# file=/tmp/test/ddr [root@localhost 20150921]# ! [ -e $file ] && mkdir -p $file || echo $file exists. && echo "file type is `file $file`" file type is /tmp/test/ddr: directory [root@localhost 20150921]# ! [ -e $file ] && mkdir -p $file || echo $file exists. && echo "file type is `file $file`" /tmp/test/ddr exists. file type is /tmp/test/ddr: directory [root@localhost 20150921]#
方法2
#!/bin/bash # #file=$1 #! [ -e $file ] && mkdir -p $file || echo $file exists. && echo "file type is `file $file`" file=$1 if ! [ -e $file ]; then mkdir -p $file echo "create file $file" else echo "$file exists." echo "file type is `file $file`" fi
执行结果:
实例2
写一个脚本,完成如下功能;判断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用read,依然如此简单)
方法1
[root@localhost 20150921]# read -p "Plz enter two integer:" num1 num2 Plz enter two integer:5 [root@localhost 20150921]# read -p "Plz enter two integer:" num1 num2 Plz enter two integer:3 5 [root@localhost 20150921]# [ $num1 -gt $num2 ] && echo "$num1 -gt $num2: num1 is greater than b" || echo "$num1 -gt $num2: num1 is not greater than b" 3 -gt 5: num1 is not greater than b [root@localhost 20150921]#
方法2
#!/bin/bash # read -p "Plz enter two integer:" num1 num2 if [ $num1 -gt $num2 ]; then echo "$num1 -gt $num2: num1 is greater than b" else echo "$num1 -gt $num2: num1 is not greater than b" fi
执行结果:
7、求100以内所有奇数之和(至少用3种方法。是的这是我们的作业^_^)
方法1
#1/bin/bash declare -i sum=0 for i in $(seq 1 2 100);do sum=$[$sum+i] done echo $sum
方法2
#!/bin/bash # declare -i sum=0 for i in {1..100};do if [ $[$i%2] -eq 1 ];then sum=$[$sum+$i] fi done echo $sum
方法3
#!/bin/bash # declare -i sum=0 declare -i i=0 while [ $i -le 100 ]; do if [ $[$i%2] -eq 1 ];then let sum+=$i fi let i++ done echo "Sum:$sum."
运行结果:
实例3
写一个脚本实现如下功能:
(1) 传递两个文本文件路径给脚本;
(2) 显示两个文件中空白行数较多的文件及其空白行的个数;
(3) 显示两个文件中总行数较多的文件及其总行数;
#!/bin/bash # for i in $1 $2;do echo $i awk '/^[ ]/ {++S[$NF]} END{for(a in S) print a, S[a]}' $i awk 'BEGIN{c=0}/^[ ]/{c++}END{printf "space=%d\ncount=%d\n",c,NR}' $i done
执行结果:
实例4
(1) 提示用户输入一个字符串;
(2) 判断:
如果输入的是quit,则退出脚本;
否则,则显示其输入的字符串内容;
#!/bin/bash # echo "enter a character:" while read char do if [ $char = 'quit' ]; then break; else echo $char echo "enter a character:" fi done
执行结果:
实例5
写一个脚本,打印2^n表;n等于一个用户输入的值;(不好意思,我调皮了)
#!/bin/bash # declare -i sum=2 read -p "Plz enter one integer: " num1 for ((j=1;j<=$num1;j++));do echo -n -e "${sum}^$j=$[$sum**$j]\n" done
执行结果:
11、写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。
#!/bin/bash # sum(){ # val=$[$num1+$num2] val=`expr $num1 + $num2` echo "Total value : $val" } gcd(){ if [ $num1 -eq $num2 ]; then echo "max is:${num1},mini is:${num1}." exit fi if [ $num1 -gt $num2 ]; then GREAT=$num1 SMALL=$num2 else GREAT=$num2 SMALL=$num1 fi declare -i GCD_RESULT=1 declare -i greattmp=1 declare -i smalltmp=1 declare -i i=1 while [ $i -le $SMALL ]; do greattmp=`expr $GREAT % $i` smalltmp=`expr $SMALL % $i` [ ${greattmp} -eq 0 ] && [ ${smalltmp} -eq 0 ] && GCD_RESULT=${i} i=`expr ${i} + 1` done #LCM_RESULT=`expr $SMALL / $GCD_RESULT` #LCM_RESULT=`expr $LCM_RESULT \* $GREAT` echo "max is:${GCD_RESULT}" } lcm(){ if [ $num1 -eq $num2 ]; then echo "max is:${num1},mini is:${num1}." exit fi if [ $num1 -gt $num2 ]; then GREAT=$num1 SMALL=$num2 else GREAT=$num2 SMALL=$num1 fi declare -i LCM_RESULT=1 declare -i greattmp=1 declare -i smalltmp=1 declare -i GCD_RESULT=1 declare -i i=1 while [ $i -le $SMALL ]; do greattmp=`expr $GREAT % $i` smalltmp=`expr $SMALL % $i` [ ${greattmp} -eq 0 ] && [ ${smalltmp} -eq 0 ] && GCD_RESULT=${i} i=`expr ${i} + 1` done LCM_RESULT=`expr $SMALL / $GCD_RESULT` LCM_RESULT=`expr $LCM_RESULT \* $GREAT` echo "mini is:${LCM_RESULT}." } prog=$(basename $0) read -p "Plz enter two integer: " num1 num2 read -p "Plz enter sum | gcd | lcm : " num case $num in sum) sum ;; gcd) gcd ;; lcm) lcm ;; *) echo "Usage:$prog sum|gcd|lcm" exit 1 esac
执行结果: