坚持#第241天~shell脚本3

来源:互联网 发布:reshade画面优化补丁 编辑:程序博客网 时间:2024/05/21 10:53

2017/12/13

取第几行head、tail -1

取行号:wc -l 文件名

 

&&是执行成功就逐条执行后面的

||是执行成功就不执行后面的了

 

bash -x 脚本名可以看到执行的过程和排错

bash -n 脚本名可以排错

++执行成功

 

 

13. 制作随机数,随机数的范围是5~50

echo $[RANDOM%46+5]

 

8个题目+3个测试(手机照片)+2个题目(点名和随机数)

 

3个小时10道题:每个小时5道题,1题12分钟

 

 

user是否存在, 若存在,输入存在,若不存在,创建用户,提示用户已经创建

利用逻辑与和逻辑或

id jim && echo"jim exit" || (useradd jim && echo "jim ok")逻辑语句太多了需要用逻辑括号(),或者使用if语句

 

写代码前配置vim编辑器:

vim ~/.vimrc

set nu

set ts=4

set sw=4

set ai

set si

set ci

nohl

 

if后面可以加命令:if id jim;then

 

题目:用if写一个创建一个目录,若存在提示已存在,若不存在就去创建并提示创建成功

#/bin/bash

#

read -p "input yourdir:" dir

if ls $dir;then

    echo "exist"

else

    mkdir $dir

    echo "creat ok"

fi

 

test是内嵌,help test

if !test -d /dir(如果不存在这个目录);then

mkdir  /dir

fi

!test -x /dir(如果这个目录不存在执行权限)

-z若字符串为空

-s文件有内容为真

-n若字符串不为空

string1=string2,!=

-a表示and

-o表示or

-eq等于

-ne不等于

[]中括号可以代替test,表达式的左右两边要写空格

[[]]两个中括号支持正则表达式&&||

test -e file 是检测文件是否存在

test -f file 是检测普通文件是否存在

[[ -e /etc/passwd &&-e /etc/ group ]]

 

id $name

if [ $? -eq 0 ];then   这个是$?的用法

  echo “$name exist”

else

  echo “no exist”

fi

 

判断字符串是否相等:

A=hello

[ $A = “hello” ]

 

课堂作业:

1.写一个脚本:

输出一个提示:请输入一个字符,当用户输入C则显示关于CPU的相关信息,当输入M则显示关于内存的信息,当输入D则显示关于磁盘的信息,当用户输入Q则退出,否则就显示帮助信息:Usage:You can input <C|M|D|Q>

解:

vim cmdq.sh

#/bin/bash

# CMDQ

read -p "inputC|M|D|Q:" order

if [ $order == "C"];then

    cat /proc/cpuinfo

elif [ $order =="M" ];then

    cat /proc/meminfo

elif [ $order =="D" ];then

    df -hT

elif [ $order =="Q" ];then

    exit 0

else

    echo "Usage:You can input<C|M|D|Q>"

fi

查看cpu信息:

lscpu或cat /proc/cpuinfo

查看内存信息:

free -m或cat /proc/meminfo

read隐藏输入问题:直接回车不输入东西的话会报错,要把这个情况考虑进去

 

 

2.匹配用户名和密码是否正确

root root

jim 123

tom 456

jack 1

解:

vim userpasswd.sh

read -p “please inputusername:” user_name

read -p “please inputpassword:” user_passwd

if [ $user_name = “root” ]&& [ $user_passwd = “root” ];then

  echo “ok”

elif [ $user_name = “jim” ]&& [ $user_passwd = “123” ];then

  echo “ok”

elif [ $user_name = “tom” ]&& [ $user_passwd = “456” ];then

  echo “ok”

elif [ $user_name = “jack” ]&& [ $user_passwd = “1” ];then

  echo “ok”

elif [ -z $user_name ] || [-z $user_passwd ]

else

  echo “no”

fi

 

7.模式匹配case?

case 命令 in

条件1)

  命令

;;

 

输出一个提示:请输入一个字符,当用户输入C则显示关于CPU的相关信息,当输入M则显示关于内存的信息,当输入D则显示关于磁盘的信息,当用户输入Q则退出,否则就显示帮助信息:Usage:You can input <C|M|D|Q>

解:

read -p “input:” string

case $string in

C)

  cat /proc/cpuinfo

  echo “echo”

;;这里是两个分号

M)

 cat /proc/meminfo

;;

D)

  df -hT

;;

Q)

  exit 0

;;

*)

  echo “please”

esac反着写表示结束

 

若是多个变量就要用双引号引起来,用空格隔开,或者直接用逗号隔开

case “$user_name  $user_passwd” in

“root  root”)

echo  “ok”;;

“jim   123”)

echo  “ok”;;

*)

    echo “”

esac

case#user_name,$user_passwd  in

root,root)

echo  “ok”;;

jim,123)

echo  “ok”;;

*)

    echo “”

esac

case语法支持[]和||

 

 

 

 

 

 

 

 

 

 

 

 

 

8.循环for   while   until?

 

9.循环控制 exit   break   continue?

 

10.expect shell脚本交互式操作?

 

11.函数?

 

12.数组?

 

13.正则表达式?

 

14.sed?

 

15.awk?

 

 

 

 

 

作业:

bilibili的马哥教育

凡哥笔记1就剩作业了(留着和吴老师发的面试题一起看)

 

交换机和路由器的关系:

网线的颜色顺序:

静态路由和动态路由:静态路由自己分配ip地址,动态路由DHCP自动分配ip地址

 

备份:系统备份(操作系统)、用户备份(文件)

每个用户的每个标识是什么?uid

圣诞树闪、颜色

 

作业:

1.判断用户输入的文件是什么文件类型,如果输入的文件不存在,则提示文件不存在

vim test1.sh

#!/bin/bash

# test

read -p “input file:” file

if [ -z $file ];then

  echo “$file is not exist”

else

  if [ $(ls -l /etc/passwd | cut-d"r" -f1) == “-” ]

echo “普通文件类型”

  elif [ $(ls -l /etc/passwd | cut-d"r" -f1) == “d” ]

echo “目录类型”

  elif [ $(ls -l /etc/passwd | cut-d"r" -f1) == “l” ]

echo “软链接类型”

  elif [ $(ls -l /etc/passwd | cut-d"r" -f1) == “s” ]

echo “套接字文件类型”

  elif [ $(ls -l /etc/passwd | cut-d"r" -f1) == “p” ]

echo “管道类型”

elif [ $(ls -l /etc/passwd | cut -d"r" -f1) == “b” ]

echo “设备块类型”

else

echo “字符类型”

  fi

fi

 

2.写一个脚本,从标准输入读取一个IP地址,要求ip不能为空,如果为空,提示你输入的为空,请重新输入,不为空那么就测试该主机是否存活,存活输出"alive",否则输入“not alive”

解:

vim /scripts/ipaddr.sh

#/bin/bash

# ip addr

read -p "input a ipaddr:" ipaddr

if [ -z $ipaddr ];then

    echo "it is null,please tryagain!"

else

    if [ $(ping -w 2 $ipaddr | grep"loss" | cut -d" " -f6) == "0%" ];then

        echo "alive"

    else

        echo "not alive"

    fi

fi保存退出

 

ping -w 2 192.168.1.61判断主机是否存活,其中-w是写几次(ping几次)

 

3.判断成绩的优良差,85~100优,60~84良,0~59为差,如果为空,提示你输入的为空,请重新输入,如果输入的不是100内的数值提示输入有误,请重新输入

解:

vim /scripts/scoreIf.sh

#/bin/bash

# score if

read -p "input yourscore:" score

if [[ $score -le"100" && $score -ge "85" ]];then

    echo "youxiu"

elif [[ $score -le"84" && $score -ge "60" ]];then

    echo "lianghao"

elif [[ $score -le"59" && $score -ge "0" ]];then

    echo "cha"

elif [ -z $score ];then

    echo "it is null,please tryagain!"

else

    echo "error input!please tryagain!"

fi保存退出

 

4.判断有多少个可登陆系统的用户,如果有说明一共有几个这样的用户,否则就说明没有这类用户

#/bin/bash

# user bin bash

userCount=$(grep -c"/bin/bash$" /etc/passwd)

if [ $userCount !="0" ];then

    echo "can login users is$userCount"

elif [ $userCount =="0" ];then

    echo "no user can login!"保存退出

 

5.判断一个数是奇数还是偶数  用if和case两种

解:

方法一,用if:

vim jioushu.sh

#/bin/bash

# ji shu he ou shu

read -p "input anumber:" num

num1=$[$num%2]

if [ $num1 == "0"];then

    echo "ou shu"

elif [ $num1 =="1" ];then

    echo "ji shu"

else

    echo "try input again!"

fi保存退出

方法二,用case:

vim jioushu2.sh

#/bin/bash

# ji shu ou shu case!

read -p "input anumber:" num

num1=$[$num%2]

case $num1 in

0)

    echo "ou shu";;

1)

    echo "ji shu";;

*)

    echo "try input again!!"

esac

 

6.为编译安装的apache写一个启动脚本  /etc/init.d/httpd start  用if和case两种

方法一,用if:

vim /scripts/apache.sh

#/bin/sbin

# apache if

chmod +x/usr/local/apache/apachectl

ln -s/usr/local/apache/apachectl /etc/init.d/httpd

read -p "inputstart|stop the apache:" enter

if [ $enter =="start" ];then

    systemctl stop httpd

   /etc/init.d/httpd start

elif [ $enter =="stop" ];then

    /etc/init.d/httpd stop

elif [ -z $enter ]

    echo "please try again!"

else

    echo "please try again!"

fi保存退出

 

方法二,用case:

vim /scripts/apache2.sh

#/bin/bash

# apache case

chmod +x /usr/local/apache/apachectl

ln -s/usr/local/apache/apachectl /etc/init.d/httpd

read -p "inputstart|stop the apache:" enter

case $enter in

start)

    systemctl stop httpd

    /etc/init.d/httpd start;;

stop)

    /etc/init.d/httpd stop;;

*)

    echo "please try again!"

esac保存退出

 

7.提示一条语句让用户输入一个文件,判断这个文件是否存在,如果不存在就说明文件不存在,如果存在就判断这个文件是不是一个普通文件,如果不是普通文件则说明文件不是普通文件,如果是则判断文件的空白行数,如果有空白行就说明有多少空白行,如果没有就说明没有空白行(提示:嵌套if)。

vim test1.sh

#!/bin/bash

# test

read -p "inputfile:" file

empty=$(grep -c"^$" $file)

if [ -z $file ];then

    echo "$file is not exist"

else

    if [ -f $file ];then

        echo "this is a normal file"

        if [ $empty == "0" ];then

            echo "it hasn't emptyrow"

        else

            echo "it has empty row,it's$empty"

        fi

    else

        echo "this is not a normalfile"

    fi

fi

 

8.写一个脚本,水果店水果价格查询
提供一个菜单
1 apple
2 banana
3 orange
用户输入 1 显示 apple 的各种类别的价格
1.1 小苹果 5.3元500g 1.2 大苹果7.8元500g 1.3 进口苹果10.8元500g
并且提示用户输入想购买哪种苹果的编号,购买多少克
当用户输入完想购买的水果输出需要多少钱


用户输入2 显示 banana 的各种类别的价格
2.1 普通香蕉 2.5元500g 2.2进口香蕉6元500g
并且提示用户输入想购买哪种香蕉的编号,购买多少克
当用户输入完想购买的水果输出需要多少钱


用户输入3显示orange的各种类别的价格
3.1 橘子 3.8元500g 3.2 皇帝柑 10元500g 3.3 甜橙 8.8元500g 3.4 砂糖橘 10.8元500g
并且提示用户输入想购买哪种香蕉的编号,购买多少克
当用户输入完想购买的水果输出需要多少钱

 

注意if [ a == 5 ]是错的,要这样写:if [ $a == 5 ],还要注意除法运算放在最后面,也就是说,if语句里面必然有$符号,bc的除法运算要注意放在最后面

 

vim food.sh

elif [ $num == "2"];then

    echo "2.1:normal banana 2.5yuan500g"

    echo "2.2:jinkou banana 6yuan500g"

    read -p "what kind of apple do youwant to buy,please enter the number:" numkind

    if [ $numkind == "2.1" ];then

        pricekind=2.5

    elif [ $numkind == "2.2" ];then

        pricekind=6

    fi

    read -p "how many do you want tobuy:" numsum

    echo"you should pay for:"

    echo"scale=2;${pricekind}*${numsum}/500" | bc

elif [ $num == "3"];then

    echo "3.1:orange 3.8yuan 500g"

    echo "3.2:huangdi orange 10yuan500g"

    echo "3.3:swit orange 8.8yuan500g"

    echo "3.4:huangdi orange 10.8yuan500g"

    read -p "what kind of apple do youwant to buy,please enter the number:" numkind

    if [ $numkind == "3.1" ];then

        pricekind=3.8

    elif [ $numkind == "3.2" ];then

        pricekind=10

    elif [ $numkind == "3.3" ];then

        pricekind=8.8

    elif [ $numkind == "3.4" ];then

        pricekind=10.8

    fi

    read -p "how many do you want tobuy:" numsum

    echo "you should pay for:"

    echo"scale=2;${pricekind}*${numsum}/500" | bc

fi

 

挂起虚拟机之后再睡眠电脑,要不然会出现ll无效、中文变成问号、执行文件没有绿色的问题

ll无效解决方案、执行文件没有绿色:设置别名:alias ll="ls-l --color=auto"

alias ls="ls --color=auto"

中文变成问号解决方案:(1) 执行locale命令查看系统语言(2)设置系统环境变量LANG为en_US.UTF-8: exportLANG=en_US.UTF-8,要想永久生效,需要vim /etc/profile回车,export LANG=en_US.UTF-8保存退出,然后执行source/etc/profile即可
原创粉丝点击