前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell 学习笔记3

shell 学习笔记3

作者头像
py3study
发布2020-01-14 16:03:16
5470
发布2020-01-14 16:03:16
举报
文章被收录于专栏:python3python3

####shell结构 #!指定执行脚本的shell #注释行 命令和控制结构  第一步:创建一个包含命令和控制结构的文件  第二步:修改这个文件的权限使它可以执行,chmod u+x  第三步:执行./example(或sh example,使用此方式执行时加-x,可以看到执行过程)

#!/bin/bash  指定执行脚本的shell程序 #This is to show  描述 echo "Our first example shell"    命令…… echo #This inserts an empty line in output. echo "We are currently in the following directory" /bin/pwd echo echo "This directory contains the following files" /bin/ls

####example.sh #!/bin/bash     执行脚本的shell #auto mail for system info 描述 /bin/date +%F >> /tmp/sysinfo 获取日期 echo "disk info:" >> /tmp/sysinfo  /bin/df -h >> /tmp/sysinfo 获取硬盘信息 echo >> /tmp/sysinfo  打印空行 echo "online users:" >> /tmp/sysinfo /usr/bin/who | /bin/grep -v root >> /tmp/sysinfo 获取在线用户(不包令root) echo >> /tmp/sysinfo echo "memory info:" >> /tmp/sysinfo 获取内存信息 /usr/bin/free -m >> /tmp/sysinfo echo >> /tmp/sysinfo #write root /usr/bin/write root < /tmp/sysinfo && /bin/rm /tmp/sysinfo # crontab -e # 0 9 * * 1-5 script  计划任务(每天早上9:00)

####shell变量  以字母或下划线开头,一般变量名为大写字母。  #位置变量:  ls -l file1 file2 file3  $0  指命令本身  $1  file1  $2  file2  ……  $n  ##特殊变量  $* 这个程序的所有参数  $# 这个程序的参数个数  $$ 这个程序的pid  $! 执行上一个后台命令的pid  $? 执行上一个命令的返回值。   ##examle    #!/bin/bash   #test special varibale   echo '$# is :' $#   echo '$* is :' $*   echo '$? is :' $?   echo '$$ is :' $$   echo '$0 is :' $0  sh example.sh file1 file2 file3 file4  ##返回结果  $# is : 4  $* is : file1 file2 file3 file4  $? is : 0  $$ is : 17379  $0 is : example

####shell命令  read:从键盘读入数据,赋值给变量   #example    #!/bin/sh    read first second third    echo "the first parameter is $first"    echo "the second parameter is $second"    echo "the third parameter is $third"  expr:对整数型变量进行算术运算,小数点后省略。   #example    expr 3 + 5    expr $var - 5     expr $var1 / $var2    expr $var3 \*10   #example2    #!/bin/bash     a=10 b=30 c=30     value1=`expr $a + $b + $c`  test:变量测试语句,用于测试变量是否相等,是否为空,文件类型等   test 测试条件    #example     test str1=str2     test strl!=str2     test -n str1 是否为空    test -d file    是否为目录    test -x file 是否可执行

   #example2     if test -d $1 then      ……     fi    test -d $1 ==== [-d $1]    #example3    #检测web服务是否启动。     #!/bin/bash      web=`/usr/bin/pgrep httpd`      if [ $web != ""]      then       echo "The web service is running"      else       echo "The web serviec in not running"              /etc/init.d/httpd start    ##if多条件判断结构  fi     if  条件1 then         命令1    elif 条件2 then         命令2    else         命令3    fi     #!/bin/bash      echo "please input a file name:"      read file_name      if [ -d $file_name ]      then       echo "$file_name is a directory"      elif [ -f $file_name ]      then       echo "$file_name is a common file"      elif [ -c -o -b $file_name ]      then       echo "$file_name is a device file"      else       echo "$file_name is an unknow file"      fi     -a 并且  -o  或    ##example     #!/bin/bash      if [$# -ne 2 ]; then       echo "Not enough parameters"       exit 0      fi      if [$1 -eq $2 ]; then       echo "$1 equals $2"      elif [ $1 -lt $2 ]; then       echo "$1 littler than $2"      elif [ $1 -gt $2 ]; then       echo "$1 greater than $2"      fi   for   done语句    格式:for 变量 in 名字表          do           命令列表          done    ##example     #!/bin/sh      for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday      do       echo "The day is $DAY"      done awk -F 域分隔符 '命令'  #example1 检测系统中UID为0的用户   awk -F: '$3==0 {print $1}' /etc/password  #example2 检测系统中密码为空的用户   awk -F: 'length($2)==0 {print $1}' /etc/shadow  #example3   #!/bin/bash   echo "Please input the username:"   read username   grep $username /etc/passwd > /dev/null 2> /dev/null   if [ $? -eq 0 ]   then    echo "username is : $username"   else    echo "user $username does not exist"    exit 1   fi    echo    #list /etc/passwd info    userinfo=`grep ^$username:x /etc/passwd`    userid=`echo $userinfo |awk -F : '{print $3}'`    groupid=`echo $userinfo |awk -F : '{print $4}'`    homedir=`echo $userinfo |awk -F : '{print $6}'`    shell=`echo $userinfo |awk -F : '{print $7}'`    #get group name from GID    grouptmpname=`cat /etc/group | grep :x:$groupid`    groupname=`echo $grouptmpname | awk -F : '{print $1}'`    echo "user id is : $userid"    echo "default group is : $groupname"    echo "home directory is : $homedir"    echo "shell is : $shell"    echo "group members info:"    #get group members    groups=`groups $username`    echo $groups    echo    #get login info    userlogin=`who | grep $username`    if [ "$userlogin" != "" ]    then     echo "$username is online"    else     echo "$username NOT logged in"    fi

###example for for  #!/bin/bash  username=$1  ps aux |grep $username |awk '{print $2}' > /tmp/temp.pid  killid=`cat /tmp/temp.pid`  for pid in $killid  do   kill -9 $pid 2> /dev/null  done select 变量 in 关键字 do  command 1  ……  command 2 done  #example  #!/bin/bash   echo "What is your favourity OS?"   select var in "Linux" "Unix" "Windows" "Other"   do    break   done   echo "You have selected $var" ##case  case ... esac 语句,格式:   case 变量 in    字符串1) 命令列表1     ;;    ...    字符串n) 命令列表n           ;;   esac  ##example   #!/bin/bash   echo "***************************"   echo "Please select your operation:"   echo "Press "C" to Copy"   echo "Press "D" to Delete"   echo "Press "B" to Backup"   echo "***************************"   read op   case $op in    c)     echo "Your selection is Copy"     ;;    D)     echo "Your selection is Delete"     ;;    B)     echo "Your selection is Backup"     ;;    *)     echo "Invalide selection"   esac while语句,格式:  while 条件  do   命令  done  #example  #!/bin/bash  num=1  while [ $num -le 10 ]  do   SUM=`expr $num \* $num`   echo $SUM   num=`expr $num + 1`  done

 #example2  #!/bin/bash  echo "please input username:"  read name  echo "please input number:"  read num  n=1  while [ $n -le $num ]  do   useradd $name$n   n=`expr $n + 1`  done  echo "please input the password:"  read passwd  m=1  while [ $m -le $num ]  do   echo $passwd |passwd --stdin $name$m   m=`expr $m + 1`  done

echo 12345 |passwd --stdin shedon    "12345"为设置的密码  shedon为用户

until语句,格式:  until 条件  do   命令  done  until类似while循环,不同的是until是条件返回值为假时才继续执行。  #example   #!/bin/bash   until [ -x /etc/inittab ]   do    /bin/ls -l /etc/inittab    exit 0   done  #example2  #!/bin/bash  echo "Press Y/y to stop..."  read input  until [ $input = "Y" ] || [ $input = y]  do   echo "error input,please try again..."   read input  done  echo "stop here!"

 #########跳出循环:break 和 continue  break  跳出循环  continue 跳出本次循环

shift指令:参数左移,每执行一次,参数向左移一位,$#的值减1,用于分别处理每个参数,移出去的参数不再可用

####函数应用  函数定义:   函数名()   {    命令序列    }  函数的调用:不带()   函数名 参数1 参数2 ... #######函数中的变量:  均为全局变量,没有局部变量 #######函数中的参数:调用函数时,可以传递参数,在函数中用$1 $2...来引用

###sh -x script  将执行脚本并显示所有变量值 ###sh -n script  不执行脚本只是检查语法模式,将返回所有语法错误。

###普通用户脚本执行权限 sh 执行 1.普通用户对脚本文件有r权限 2.对脚本所在目录有rx权限

脚本直接执行 1.普通用户对脚本文件有rx权限 2.对脚本所在目录有rx权限

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档