@H_502_0@新手写的shell,写的不好,请大家见谅。
@H_502_0@希望结交一些大神和同行。QQ:86416192 欢迎大家加QQ。
@H_502_0@1、写一个脚本,显示出来多少个用户,并且显示出来每个用户的ID。
@H_502_0@#!/bin/bash
@H_502_0@
@H_502_0@file="/etc/passwd"
@H_502_0@LINES=`wc -l $file | cut -d" " -f1`
@H_502_0@for I in `seq 1 $LINES`;
@H_502_0@do
@H_502_0@userid=`head -$I $file | tail -1 |cut -d: -f3`
@H_502_0@username=`head -$I $file | tail -1 |cut -d: -f1`
@H_502_0@echo "hello $username,your UID is $userid"
@H_502_0@done
@H_502_0@echo "there are $LINES users"
@H_502_0@
@H_502_0@测试结果
@H_502_0@[root@backup01 scripts]# sh test.sh
@H_502_0@hello root,your UID is 0
@H_502_0@hello bin,your UID is 1
@H_502_0@hello daemon,your UID is 2
@H_502_0@hello adm,your UID is 3
@H_502_0@hello lp,your UID is 4
@H_502_0@hello sync,your UID is 5
@H_502_0@hello shutdown,your UID is 6
@H_502_0@hello halt,your UID is 7
@H_502_0@hello mail,your UID is 8
@H_502_0@hello operator,your UID is 11
@H_502_0@hello games,your UID is 12
@H_502_0@hello ftp,your UID is 14
@H_502_0@hello nobody,your UID is 99
@H_502_0@hello avahi-autoipd,your UID is 170
@H_502_0@hello systemd-bus-proxy,your UID is 999
@H_502_0@hello systemd-network,your UID is 998
@H_502_0@hello dbus,your UID is 81
@H_502_0@hello polkitd,your UID is 997
@H_502_0@hello abrt,your UID is 173
@H_502_0@hello tss,your UID is 59
@H_502_0@hello postfix,your UID is 89
@H_502_0@hello sshd,your UID is 74
@H_502_0@hello rsync,your UID is 1000
@H_502_0@hello chrony,your UID is 996
@H_502_0@there are 24 users
@H_502_0@
@H_502_0@
@H_502_0@
@H_502_0@2、在根目录下有四个文件m1.txt,m2.txt,m3.txt,m4.txt,用Shell编程,实现自动创建m1,m2,m3,m4四个目录,并将m1.txt,m2.txt,m3.txt,m4.txt四个文件分别拷贝到各自相应的目录下。
@H_502_0@#!/bin/bash
@H_502_0@cd /
@H_502_0@touch m1.txt m2.txt m3.txt m4.txt
@H_502_0@I=1
@H_502_0@while [ $I -le 4 ]
@H_502_0@do
@H_502_0@mkdir m$I
@H_502_0@cp m$I.txt m$I
@H_502_0@I=$((I+1))
@H_502_0@done
@H_502_0@
@H_502_0@
@H_502_0@
@H_502_0@3、编写一个名为myfirstshell.sh的脚本,它包括以下内容。
@H_502_0@a) 包含一段注释,列出您的姓名、脚本的名称和编写这个脚本的目的。
@H_502_0@b) 问候用户。
@H_502_0@c) 显示日期和时间。
@H_502_0@d) 显示这个月的日历。
@H_502_0@e) 显示您的机器名。
@H_502_0@f) 显示当前这个操作系统的名称和版本。
@H_502_0@g) 显示父目录中的所有文件的列表。
@H_502_0@h) 显示root正在运行的所有进程。
@H_502_0@i) 显示变量TERM、PATH和HOME的值。
@H_502_0@j) 显示磁盘使用情况。
@H_502_0@k) 用id命令打印出您的组ID。
@H_502_0@m) 跟用户说“Good bye”
@H_502_0@
@H_502_0@
@H_502_0@#!/bin/bash
@H_502_0@echo "李松阳编写"
@H_502_0@user=`whoami`
@H_502_0@case $user in
@H_502_0@root)
@H_502_0@echo "hello root";;
@H_502_0@teacher)
@H_502_0@echo "hello teacher";;
@H_502_0@*)
@H_502_0@echo "hello $user,welcome"
@H_502_0@esac echo "日期和时间: `date`"
@H_502_0@echo "本月的日历: `cal`"
@H_502_0@echo "本机的机器名:`uname -n`"
@H_502_0@echo "当前这个操作系统的名称和版本:`uname -s;uname -r`"
@H_502_0@echo "父目录中的所有文件的列表:`ls ../`"
@H_502_0@echo "root正在运行的所有进程:` ps -u root`"
@H_502_0@echo "变数TERM的值:$TERM"
@H_502_0@echo "变数PATH的值:$PATH"
@H_502_0@echo "变数HOME的值:$HOME"
@H_502_0@echo "磁盘的使用情况:`df`"
@H_502_0@echo "用id命令打印出你的组ID:`id -g`"
@H_502_0@echo "Good bye!"
@H_502_0@
@H_502_0@
@H_502_0@
@H_502_0@4、设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限为 rwxr-xr―
@H_502_0@
@H_502_0@#!/bin/bash
@H_502_0@
@H_502_0@if [ -d /userdata ]
@H_502_0@ then
@H_502_0@ cd /userdata
@H_502_0@ else
@H_502_0@ mkdir -p /userdata && cd /userdata
@H_502_0@fi
@H_502_0@
@H_502_0@I=1
@H_502_0@
@H_502_0@while [ $I -le 50 ]
@H_502_0@do
@H_502_0@mkdir -p user$I
@H_502_0@chmod 754 user$I
@H_502_0@I=$((I+1))
@H_502_0@done
@H_502_0@
@H_502_0@5、编写shell程序,实现自动创建50个用户账号的功能。账号名为stud1至stud50。
@H_502_0@#!/bin/bash
@H_502_0@
@H_502_0@I=1
@H_502_0@
@H_502_0@while [ $I -le 50 ]
@H_502_0@do
@H_502_0@useradd stud$I
@H_502_0@I=$((I+1))
@H_502_0@done
@H_502_0@
@H_502_0@6、编写shell程序,实现自动删除50个用户账号的功能。账号名为stud1至stud50。
@H_502_0@[root@backup01 scripts]# cat test06.sh
@H_502_0@#!/bin/bash
@H_502_0@
@H_502_0@I=1
@H_502_0@
@H_502_0@while [ $I -le 50 ]
@H_502_0@do
@H_502_0@userdel stud$I
@H_502_0@I=$((I+1))
@H_502_0@done