第8章 判断 & 第7章练习

1 编写一个名为rename的程序,给第1个参数所给定的文件更名,在原名后添加第2个参数包含的一串字符。
即rename memol   .sv   
应该将文件memol更名为memol.sv
//key:
[root@localhost ~]# cat test
mv $1 $1$2
[root@localhost ~]# chmod +x test
[root@localhost ~]# ls
nu
[root@localhost ~]# ./test nu .txt
[root@localhost ~]# ls
nu.txt  
[root@localhost ~]#
2编写一个名为unrename的程序,从第1个参数指定的文件名的后部去掉第2个参数包含的字符串,即
    unrename memol.sv   .sv
     应该把文件memol.sv更名为memol。要保证从尾部去掉字 符串,如    
    unrename test1test  test
    应该将tes1test更名为test1(提示:用sed和命令替换)
//key:
mv  $1 $1|sed  ‘s/$2//’     即将实参1,含有实参2的地方去掉

 [soflib@localhost ~]$ cat key2
echo "$1"
#$1 | sed 's/$2//'
del=$( echo $1 | sed "s/$2//")
echo $del
mv $1 $del
[soflib@localhost ~]$




第8章判断
p4
[root@localhost test01]# cat on
user="$1"
if who|grep "^$user"
then
  echo "$user is logged on"
fi
[root@localhost test01]# chmod +x on
[root@localhost test01]# ./on root
root     tty1         2010-04-22 14:46 (:0)
root     pts/0        2010-04-22 14:54 (:0.0)
root is logged on
[root@localhost test01]#

p8
[root@localhost test01]# x1="005"
[root@localhost test01]# [ "$x1" = 5 ]
[root@localhost test01]# echo $?
1
[root@localhost test01]# [ "$x1" -eq 5 ]
[root@localhost test01]# echo $?
0
[root@localhost test01]#

p11
[root@localhost test01]# cat file
if [ ! -e "$1" ]
 then
     echo "$1 do not exist"
fi
[root@localhost test01]# ./file on
[root@localhost test01]# echo $?
0
[root@localhost test01]#


p16
[root@localhost test01]# cat greetings
hour=$(date |cut -c 19-20)
if [ "$hour" -ge 0 -a "$hour" -le 11 ]
then
  echo "GoodMoring"
else
  if [ "$hour" -ge 12 -a "$hour" -le 17 ]
    then
       echo "Good afternoon"
     else
       echo "good evening"
    fi
fi
[root@localhost test01]# ./greetings
Good afternoon
[root@localhost test01]#


[root@localhost test01]# cat g2
hour=$(date |cut -c 19-20)
if [ "$hour" -ge 0 -a "$hour" -le 11 ]
then
   echo "gm"
  elif [ "$hour" -ge 12 -a "$hour" -le 17 ]
   then
   echo "ga"
  else
     echo "ge"
fi

[root@localhost test01]# chmod +x g2
[root@localhost test01]# ./g2
ga
[root@localhost test01]#


p18
[root@localhost test01]# cat number
if [ "$#" -ne 1 ]
then
  echo "老大请输入数字(谢谢)"
  exit 1
fi
case "$1"
 in
   0) echo zero;;
  1)echo one;;
2)echo two;;
3)echo three;;
4)echo four;;
5)echo five;;
6)echo six;;
7)echo seven;;
8)echo eight;;
9)echo nine;;
esac
[root@localhost test01]# ./number
老大请输入数字(谢谢)
[root@localhost test01]# ./number 8
eight
[root@localhost test01]#

//第二版
[root@localhost test01]# cat number
if [ "$#" -ne 1 ]
then
  echo "老大请输入数字(谢谢)"
  exit 1
fi
case "$1"
 in
   0) echo zero;;
  1)echo one;;
2)echo two;;
3)echo three;;
4)echo four;;
5)echo five;;
6)echo six;;
7)echo seven;;
8)echo eight;;
9)echo nine;;
*)echo "参数错误阿兄台:请用意味数字";;
esac
[root@localhost test01]# ./number 22
参数错误阿兄台:请用意味数字
[root@localhost test01]#

写一个小程序,通过参数得到一个字符,判断字符的类型。(数字,小写字母,大写字母和特殊字符)
p21

[soflib@localhost ~]$ cat ctype
if [ "$#" -ne 1 ]
then
echo "用法:ctype 字符"
exit 1
fi
char="$1"
echo $char
numchar=$(echo -n $char | wc -c )
echo "$numchar"
if [ "$numchar" -ne 1 ]
then
     echo "请键入单字"
exit 1
fi
case "$char"
in
[0-9]) echo "数字";;
[a-z]) echo "小写字母";;
[A-Z]) echo "大写字母";;
*) echo "特殊字符";;
esac
[soflib@localhost ~]$ bash ctype 34
34
2
请键入单字
[soflib@localhost ~]$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值