第七章+第八章 参数传递和判断

1.$#[soflib@localhost ~]$ vi yl
echo $# arguments passed
echo arg 1=:$1,arg 2=:$2,arg 3= :$3
[soflib@localhost ~]$ chmod +x yl
[soflib@localhost ~]$ ./yl a b c
3 arguments passed
arg1=:a,arg2=:b,arg3=:c
$*
2.[soflib@localhost ~]$ vi yl2
[soflib@localhost ~]$ chomd +x yl2
bash: chomd: command not found
[soflib@localhost ~]$ chmod +x yl2
[soflib@localhost ~]$ ./yl 2 a b c
3arguments passed
they are:a b c
${n}
3.[soflib@localhost ~]$ vi yl3
[soflib@localhost ~]$ chmod +x yl3
[soflib@localhost ~]$ ./yl 3 a b c d e f g h i j k
11arguments passed
they are:a,b,c,d,e,f,g,h,i,j,k
4.[soflib@localhost ~]$ vi m1.c
[soflib@localhost ~]$ cat m1.c
main(){
printf("Begin/n");
}
[soflib@localhost ~]$ vi m2.c
[soflib@localhost ~]$ cat m2.c
#include<stdio.h>
main(){
printf("OK!/n");
}
[soflib@localhost ~]$ vi CYY
[soflib@localhost ~]$ cat CYY
cat $1 $2 |wc -l
[soflib@localhost ~]$ chmod +x CYY
[soflib@localhost ~]$ ./CYY m1.c m2.c
7

判断

显示指定用户是否登录

1.[soflib@localhost ~]$ vi on
[soflib@localhost ~]$ cat on
user="$1"
if who |grep "$user" >/dev/null
then
    echo "$user is logged on"
fi
[soflib@localhost ~]$ chmod +x on
[soflib@localhost ~]$ ./on soflib
soflib is logged on
 
2.[soflib@localhost ~]$ x1="005"           
比较

[soflib@localhost ~]$ [ "$x1" = 5 ]
[soflib@localhost ~]$ echo $?
1
[soflib@localhost ~]$ [ "$x1" = 005 ]
[soflib@localhost ~]$ echo $?
0
[soflib@localhost ~]$ [ "$x1" -eq 5 ]
[soflib@localhost ~]$ echo $?
0

3.[soflib@localhost ~]$ [ "$x1"!="$2" ]       
逻辑非操作符    对表达式求反
[soflib@localhost ~]$ echo $?
0
[soflib@localhost ~]$ [ ! -f "$mialfile" ]
[soflib@localhost ~]$ echo $?
0
[soflib@localhost ~]$ [ ! "$x1"="$2" ]
[soflib@localhost ~]$ echo $?
1

4.[soflib@localhost ~]$ cat file
$cat file
if [ ! -e "$0" ] then
   echo "$0 do not exist"
fi

[soflib@localhost ~]$ ls
1.c          employees.txt   m1.c        tot                    
模板
1.c~         ep              m2.c        tt                     
视频
a.out        epp             on          UNIX
操作系统简介.doc    图片
args         express.txt     POSIX.doc   yan.exe                
文档
awkfile      file            process     yl                     
下载
awkfile.txt  first.c         process.c   yl2                    
新文件
cts          first.c~        process.c~  yl3                    
新文件~
cts.c        first.exe       second.c   
公共的                  音乐

cts.c~       free
的含义.doc  second.c~   介绍几个Unix版本.doc    桌面
CYY          grade           second.exe 
介绍几个UNIX的变种.doc
[soflib@localhost ~]$ file
文档

文档: directory


5.[soflib@localhost ~]$ count=12
[soflib@localhost ~]$ [ "$count" -ge 0 -a "$count" -lt 10 ]   0-10
之间的数
[soflib@localhost ~]$ echo $?
1
[soflib@localhost ~]$ count=5
[soflib@localhost ~]$ [ "$count" -ge 0 -a "$count" -lt 10 ]    0
10之间的数
[soflib@localhost ~]$ echo $?
0

6.[soflib@localhost ~]$ [ /( "$count" -ge 0 /) -a /( "$count" -lt 10 /) ]
[soflib@localhost ~]$ echo $?
1
[soflib@localhost ~]$ count=6
[soflib@localhost ~]$ [ /( "$count" -ge 0 /) -a /( "$count" -lt 10 /) ]
[soflib@localhost ~]$ echo $?
0

7.[softlib@localhost ~]$ vi yl

[softlib@localhost ~]$ cat yl
hour=$(date |cut -c 19-20)
if [ "$hour" -ge 0 -a  "$hour" -le 11 ]
then
echo "Good moring"
else
if [ "$hour" -ge 12 -a "$hour" -le 17 ]
then
echo "Good afternoon"
else
echo "Good evening"
fi
fi
[softlib@localhost ~]$ ./yl
Good afternoon

8.[softlib@localhost ~]$ cat greetings
hour=$(date |cut -c 19-20)
if [ "$hour" -ge 0 -a  "$hour" -le 11 ]
then
          echo "Good moring"
elif [ "$hour" -ge 12 -a "$hour" -le 17 ]
then
          echo "Good afternoon"
else
          echo "Good evening"
fi
[softlib@localhost ~]$ ./greetings
Good afternoon

9.[soflib@localhost ~]$ vi yl1
[soflib@localhost ~]$ chmod +x yl1
[soflib@localhost ~]$ cat yl1
if [ "$#" - ne 1 ]
then
    echo "
用法:number 数字

    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
[soflib@localhost ~]$ ./yl1 6
six

10..[soflib@localhost ~]$ vi yl2
[soflib@localhost ~]$ cat yl2
if [ "$#" -ne 1 ]
then
    echo "
用法:number 数字
"
    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
[soflib@localhost ~]$ chmod +x yl2
[soflib@localhost ~]$ ./yl2 5
five
[soflib@localhost ~]$ ./yl2 52
参数错误;请用一位数字

11..写一个小程序,通过参数得到一个字符,判断字符的类型。(数字,小写字母,大写字母和特殊字符)
[soflib@localhost ~]$ vi cttpe
[soflib@localhost ~]$ cat ctype
if [ "$#" -ne 1 ]
then
    echo "
用法:ctye 字符"
    exit 1
fi
  char="$1"
  number=$(echo -n $char | wc -c)
echo $char
  if [ "$number" -ne 1 ]
  then
echo $char
      echo "
请输入单字符
"
   exit 1
echo $char 
 case "$char"
   in
   [0-9]) echo "
数字
";;
   [a-z]) echo "
小写字母
";;
   [A-Z]) echo
大写字母
;;
   *)echo
特殊字符
;;
  esac
 fi
[soflib@localhost ~]$ vi ctype
[soflib@localhost ~]$ ./ctype b
b
b

小写字母

 

12. [softlib@localhost ~]$ vi greetings2
[softlib@localhost ~]$ cat greetings2
Hour=$(date +%H)
case "$Hour" in
0?|1[0-1])echo "good morning";;
1[2-7])echo "good afternoon";;
*)echo "good evening";;
esac
[softlib@localhost ~]$ ./greetings2
good afternoon

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值