shell脚本练习

前端之家收集整理的这篇文章主要介绍了shell脚本练习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

新手写的shell,写的不好,请大家见谅。

希望结交一些大神和同行。QQ:86416192 欢迎大家加QQ。

1、写一个脚本,显示出来多少个用户,并且显示出来每个用户的ID。

#!/bin/bash


file="/etc/passwd"

LINES=`wc -l $file | cut -d" " -f1`

for I in `seq 1 $LINES`;

do

userid=`head -$I $file | tail -1 |cut -d: -f3`

username=`head -$I $file | tail -1 |cut -d: -f1`

echo "hello $username,your UID is $userid"

done

echo "there are $LINES users"


测试结果

[root@backup01 scripts]# sh test.sh

hello root,your UID is 0

hello bin,your UID is 1

hello daemon,your UID is 2

hello adm,your UID is 3

hello lp,your UID is 4

hello sync,your UID is 5

hello shutdown,your UID is 6

hello halt,your UID is 7

hello mail,your UID is 8

hello operator,your UID is 11

hello games,your UID is 12

hello ftp,your UID is 14

hello nobody,your UID is 99

hello avahi-autoipd,your UID is 170

hello systemd-bus-proxy,your UID is 999

hello systemd-network,your UID is 998

hello dbus,your UID is 81

hello polkitd,your UID is 997

hello abrt,your UID is 173

hello tss,your UID is 59

hello postfix,your UID is 89

hello sshd,your UID is 74

hello rsync,your UID is 1000

hello chrony,your UID is 996

there are 24 users




2、在根目录下有四个文件m1.txt,m2.txt,m3.txt,m4.txt,用Shell编程,实现自动创建m1,m2,m3,m4四个目录,并将m1.txt,m2.txt,m3.txt,m4.txt四个文件分别拷贝到各自相应的目录下。

#!/bin/bash

cd /

touch m1.txt m2.txt m3.txt m4.txt

I=1

while [ $I -le 4 ]

do

mkdir m$I

cp m$I.txt m$I

I=$((I+1))

done




3、编写一个名为myfirstshell.sh的脚本,它包括以下内容

a) 包含一段注释,列出您的姓名、脚本的名称和编写这个脚本的目的。

b) 问候用户

c) 显示日期和时间。

d) 显示这个月的日历。

e) 显示您的机器名。

f) 显示当前这个操作系统的名称和版本。

g) 显示父目录中的所有文件的列表。

h) 显示root正在运行的所有进程。

i) 显示变量TERM、PATH和HOME的值。

j) 显示磁盘使用情况。

k) 用id命令打印出您的组ID。

m) 跟用户说“Good bye”



#!/bin/bash

echo "李松阳编写"

user=`whoami`

case $user in

root)

echo "hello root";;

teacher)

echo "hello teacher";;

*)

echo "hello $user,welcome"

esac echo "日期和时间: `date`"

echo "本月的日历: `cal`"

echo "本机的机器名:`uname -n`"

echo "当前这个操作系统的名称和版本:`uname -s;uname -r`"

echo "父目录中的所有文件的列表:`ls ../`"

echo "root正在运行的所有进程:` ps -u root`"

echo "变数TERM的值:$TERM"

echo "变数PATH的值:$PATH"

echo "变数HOME的值:$HOME"

echo "磁盘的使用情况:`df`"

echo "用id命令打印出你的组ID:`id -g`"

echo "Good bye!"




4、设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限为 rwxr-xr―


#!/bin/bash


if [ -d /userdata ]

then

cd /userdata

else

mkdir -p /userdata && cd /userdata

fi


I=1


while [ $I -le 50 ]

do

mkdir -p user$I

chmod 754 user$I

I=$((I+1))

done


5、编写shell程序,实现自动创建50个用户账号的功能。账号名为stud1至stud50。

#!/bin/bash


I=1


while [ $I -le 50 ]

do

useradd stud$I

I=$((I+1))

done


6、编写shell程序,实现自动删除50个用户账号的功能。账号名为stud1至stud50。

[root@backup01 scripts]# cat test06.sh

#!/bin/bash


I=1


while [ $I -le 50 ]

do

userdel stud$I

I=$((I+1))

done

原文链接:https://www.f2er.com/bash/391348.html

猜你在找的Bash相关文章