鸟哥的Linux私房菜 学习 Shell Scripts

第十三章、学习 Shell Scripts

重点回顾

  • shell script 是利用 shell 的功能所写的一个『程序 (program)』,这个程序是使用纯文字档,将一些 shell 的语法与命令(含外部命令)写在里面, 搭配正规表示法、管线命令与数据流重导向等功能,以达到我们所想要的处理目的

更多参考http://cn.linux.vbird.org/linux_basic/0340bashshell-scripts.php#hint

本章习题

  • 请创建一支 script ,当你运行该 script 的时候,该 script 可以显示: 1. 你目前的身份 (用 whoami) 2. 你目前所在的目录 (用 pwd)

1_whoami.sh

#!/bin/bash

echo "目前的身份: $(whoami)"
echo "目前所在的目录: $(pwd)"

  • 请自行创建一支程序,该程序可以用来计算『你还有几天可以过生日』啊
    2_date.sh
 now=$(date +%m%d)
if [ "$birthday" == "$now" ]; then
    echo "Happy birthday to you!"
elif [ "$birthday" -gt "$now" ]; then
    year=$(date +%Y)
    total_d=$(($((`date --date="$year$birthday" +%s`-`date +%s`))/60/60/24))
    echo "Your birthday will be $total_d later"
else
    year=$(($(date +%Y)+1))
    total_d=$(($((`date --date="$year$birthday" +%s`-`date +%s`))/60/60/24))
    echo "Your birthday will be $total_d later"
fi

在这里插入图片描述
参考答案

#!/bin/bash
read -p "Pleas input your birthday (MMDD, ex> 0709): " bir
now=`date +%m%d`
if [ "$bir" == "$now" ]; then
    echo "Happy Birthday to you!!!"
elif [ "$bir" -gt "$now" ]; then
    year=`date +%Y`
    total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
    echo "Your birthday will be $total_d later"
else
    year=$((`date +%Y`+1))
    total_d=$(($((`date --date="$year$bir" +%s`-`date +%s`))/60/60/24))
    echo "Your birthday will be $total_d later"
fi

在这里插入图片描述

  • 让使用者输入一个数字,程序可以由 1+2+3… 一直累加到使用者输入的数字为止。
    3_while.sh
#!/bin/bash

read -p "输入一个数字: " n
s=0 #累加值
i=0

# while [ $i != $n ]
while [ $i -lt "$n" ] # -lt <
do
	i=$((i+1))
	s=$((s+i))
done

echo "The result of '1+2+3+...+$n' is ==> $s"

3_for.sh

#!/bin/bash

read -r -p "输入一个数字: " n
s=0 #累加值

for i in $(seq 1 $n)
do
	s=$((s+i))
done

echo "The result of '1+2+3+...+$n' is ==> $s"

3_for_c.sh

#!/bin/bash

read -r -p "输入一个数字: " n
s=0 #累加值

for (( i=1;i<=n;i+=1 ))
do
	s=$((s+i))
done

echo "The result of '1+2+3+...+$n' is ==> $s"

在这里插入图片描述

  • 撰写一支程序,他的作用是: 1.) 先查看一下 /root/test/logical 这个名称是否存在; 2.) 若不存在,则创建一个文件,使用 touch 来创建,创建完成后离开; 3.) 如果存在的话,判断该名称是否为文件,若为文件则将之删除后创建一个目录,档名为 logical ,之后离开; 4.) 如果存在的话,而且该名称为目录,则移除此目录!
    4_file.sh
#!/bin/bash

name='logical' #'/root/test/logical'
if [ -e $name ]  #查看一下名称是否存在
then
    if [ -f $name ]  #如果存在的话,判断该名称是否为文件
    then
        #若为文件则将之删除后创建一个目录
        rm -f $name
        mkdir $name
    elif [ -d $name ]    #如果存在的话,而且该名称为目录,则移除此目录
    then
        rm -rf $name
    fi
else
    touch $name  #若不存在,则创建一个文件
fi

在这里插入图片描述
参考答案

#!/bin/bash
if [ ! -e logical ]; then
    touch logical
    echo "Just make a file logical"
    exit 1
elif [ -e logical ] && [ -f logical ]; then
    rm logical
    mkdir logical
    echo "remove file ==> logical"
    echo "and make directory logical"
    exit 1
elif [ -e logical ] && [ -d logical ]; then
    rm -rf logical
    echo "remove directory ==> logical"
    exit 1
else
    echo "Does here have anything?"
fi

在这里插入图片描述

  • 我们知道 /etc/passwd 里面以 : 来分隔,第一栏为帐号名称。请写一只程序,可以将 /etc/passwd
    的第一栏取出,而且每一栏都以一行字串『The 1 account is “root” 』来显示,那个 1 表示行数。
#!/bin/bash

# users=$(cut -d ':' -f1 /etc/passwd)
users=$(cat /etc/passwd | cut -d ':' -f1)
num=0
for username in $users
do
    (( num=num+1 ))
    echo "The $num account is \"$username\""  
done

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值