linuxshell 读书笔记二 (unix shell 第三版)


who | grep fred                --查找fred用户是否登录
echo  $?                       --上一条命令成功返回0 否则不为0

[xep@localhost ~]$ cat logon_script
user="$1"
if who | grep "^$user ">/dev/null
 then
   echo "$user is logged on "
fi

./logon_script xep             --用户xep是否登录

--test 命令接收三个参数字符串 或是 一个字符串,三个字符串时候是判断字符串1和3是否与操作符2匹配
    一个参数字符串的时候  判断 不为空就返回真
[xep@localhost ~]$ test name='s'
[xep@localhost ~]$ echo $?
0
[xep@localhost ~]$ test name = 's'
[xep@localhost ~]$ echo $?
1
字串操作符
test -n string                 --string 不为空为真
test -z strng                  --string 空为真
test "s1" = "s2"               --判断两个串是否相等

整形的操作符
[xep@localhost ~]$ cat -n int_max_script

     1  #! /bin/bash
     2  #  compare two parameter
     3  #  echo    the maxer
     4  if [ $# -lt 2 ]; then
     5     echo "this script input two parameter  at leastet!"
     6     exit 0
     7  fi
     8
     9  if [ $1 -eq $2 ]
    10  then
    11     echo "two parameter equate $1 "
    12     exit 0
    13  fi
    14
    15  if test $1 -gt $2
    16  then
    17     echo "$1 is greater than $2"
    18  elif (($1<$2 ))
    19  then
    20     echo "$2 is greater than $1"
    21  else
    22     echo "$1 is less than $2"
    23  fi


修改脚本属性  测试结果
[xep@localhost ~]$ ./int_max_script
this script input two parameter  at leastet!

[xep@localhost ~]$ ./int_max_script 2
this script input two parameter  at leastet!

[xep@localhost ~]$ ./int_max_script 3 2
3 is greater than 2

[xep@localhost ~]$ ./int_max_script 2 3
3 is greater than 2

[xep@localhost ~]$ ./int_max_script 2 , 3
./int_max_script: line 9: [: ,: integer expression expected
./int_max_script: line 15: test: ,: integer expression expected
2 is less than ,

[xep@localhost ~]$ ./int_max_script 005  5
two parameter equate 005

--在上面的例子中也可以看到 test 命令 与 [ ] 是一样的 最后一测试说明有隐式类型转换
--加上 elif 完全是为演示在整形中比较可以用 (( )) 方式 与算术运算符结合使用
--把elif去掉 程序完全可用
--if 和 then 放在一行时 中间一定要加 ; (分号)
--对几个符号说一下 -ne not equate(不等于)        -eq  equate(相等)     -gt great than (大于)
--                 -ge great equate(大于等于)         -lt  less than(小于)  -le less equate(小于等于)
--等学会异常处理的时候 报的这些错就可以按自定义的结果输出

[xep@localhost ~]$ cat -n if_else_script
     1  #! /bin/bash
     2  #  check file type display
     3
     4  if [ $# -lt 1 ]
     5  then
     6     echo "must input one file_name at least!"
     7     exit 1
     8  fi
     9
    10  while [ $# -gt 0 ]
    11  do
    12    if [ -f $1 ] ; then
    13       if [ -x $1 ] ; then
    14          echo " $1 is a execute file"
    15       fi
    16       if [ -r $1  ]; then
    17          echo " $1 is can read file "
    18       fi
    19       if [ -w $1 ] ; then
    20          echo " $1 is can write file"
    21       fi
    22       echo "$1 is a file in this direcotry"
    23    elif [ -d $1 ]
    24       then
    25       echo "$1 is a directory"
    26    else
    27       echo "there not exist $1 file"
    28    fi
    29   shift
    30  done
 
修改脚本属性  测试结果     
[xep@localhost ~]$ ./if_else_script test1
 test1 is can read file
 test1 is can write file
test1 is a file in this direcotry

[xep@localhost ~]$ ./if_else_script xep
xep is a directory

[xep@localhost ~]$ ./if_else_script
must input one file_name at least!

[xep@localhost ~]$ ./if_else_script pas  test1 test.123   xep
 pas is can read file
 pas is can write file
pas is a file in this direcotry
 test1 is can read file
 test1 is can write file
test1 is a file in this direcotry
 test.123 is can read file
 test.123 is can write file
test.123 is a file in this direcotry
xep is a directory

--上面的例子展示了一些文件操作的操作符 简单说一下    -f 普通文件 -d 目录
--    -e  是否存在  -r 是否有权限读  -w 是否有权限写 -x 是否有权限执行
--新建一用户将新用户与xep用户设为一组
useradd -g 500   yp
        组 组ID  用户名

[yp@localhost xep]$ ls -l
total 164
-rwxr-xr-x 1 xep xep   53 Oct 29 09:30 args2_script
-rwxr-xr-x 1 xep xep   72 Oct 29 09:39 args3_script
-rwxr-xr-x 1 xep xep   63 Oct 29 09:21 args_script
-rwxr-xr-x 1 xep xep   48 Oct 29 07:06 cmd_script
-rwxr-xr-x 1 xep xep  150 Oct 29 06:05 echo_script
-rwxr-xr-x 1 xep xep  542 Oct 29 15:18 if_else_script
-rwxr-xr-x 1 xep xep  374 Oct 29 12:13 int_max_script
-rw-rw-r-- 1 xep xep  266 Oct 29 02:14 intro
-rw-rw-r-- 1 xep xep  261 Oct 29 00:38 introbak
-rwxr-xr-x 1 xep xep   84 Oct 29 10:08 logon_script
-rw-rw-r-- 1 xep xep    0 Oct 29 06:31 null
-rw--w-r-- 1 xep xep   50 Oct 29 00:25 pas
-rw--w-r-- 1 xep xep  134 Oct 28 23:44 phonebook
-rw--w-r-- 1 xep xep   19 Oct 29 05:30 test1
-rw-rw-r-- 1 xep xep    9 Oct 29 05:40 test11
-rw-rw-r-- 1 xep xep   50 Oct 28 00:19 test.123
-rw-rw-r-- 1 xep xep  107 Oct 27 23:41 test1andlink
-rw-rw-r-- 1 xep xep   57 Oct 27 23:29 testlink
-rw-rw-r-- 1 xep xep   57 Oct 28 00:19 testlink.1
-rwxr-xr-x 1 xep xep  139 Oct 29 06:31 variable_script
drwx-wxr-x 3 xep xep 4096 Oct 29 06:25 xep
[yp@localhost xep]$ ./if_else_script pas  test1 test.123   xep
 pas is can write file
pas is a file in this direcotry
 test1 is can write file
test1 is a file in this direcotry
 test.123 is can read file
 test.123 is can write file
test.123 is a file in this direcotry
xep is a directory
[yp@localhost xep]$ ./if_else_script logon_script
 logon_script is a execute file
 logon_script is can read file
logon_script is a file in this direcotry
       

你可能感兴趣的:(shell,unix,File,Integer,读书,input)