bash脚本示例2

墨蓝 2022-01-10 10:55 317阅读 0赞

1、编写脚本计算/etc/passwd文件中第10个用户和第20个用户id号之和。

#!/bin/bash

#

a=$(head /etc/passwd | tail -1 | cut -d : -f 3)

b=$(head -20 /etc/passwd | tail -1 | cut -d : -f 3)

echo $[a+b]

2、将当前主机名保存至hostName变量中,主机名如果为空,或者为localhost.localdomain,则将其设置为www.magedu.com

#!/bin/bash

#

hostName=$(hostname)

[ -z $hostName -o $hostName == localhost.localdomain -o $hostName == localhost ] && hostname www.magedu.com

3、编写脚本,通过命令行参数传入一个用户名,判断uid是偶数还是奇数。

#!/bin/bash

#

uid=$(grep “^$1\>” /etc/passwd | cut -d : -f 3)

remainder=$[$uid % 2]

if [ $remainder == 0 ]; then

  1. echo "The uid of user $1 is even."

else

  1. echo "The uid of user $1 is odd."

fi

转载于:https://blog.51cto.com/3653220/2356406

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读

    相关 bash脚本执行的控制语句

    一、bash脚本执行的控制语句: 1、顺序执行:默认,逐条执行各语句。 2、选择执行if:分支,条件判断,执行符合条件的分支。 (1)、单分支: