-
用脚本添加10个用户
#!/bin/bash 2 3 for i in {1..10};do 4 if ! id user$i &> /dev/null ;then 5 useradd user$i 6 echo user$i | passwd --stdin user$i &> /dev/null 7 echo "add user$i finished" 8 else 9 echo "user$i exists" 10 fi 11 12 done
-
删除用户
1 #!/bin/bash 2 3 for I in {1..10};do 4 if id user$I &> /dev/null ;then 5 userdel -r user$I 6 echo "del user$I finish" 7 else 8 echo "user$I not exists" 9 fi 10 done
-
添加或删除用户
#!/bin/bash 2 3 if [ $# -lt 1 ];then 4 echo "Uage : adminusers ARG" 5 exit 1 6 fi 7 if [ $# -gt 1 ];then 8 echo "too many argument" 9 exit 2 10 fi 11 12 if [ $1 == ‘add‘ ];then 13 for I in {1..10};do 14 if id user$I &> /dev/null;then 15 echo "user$I exists" 16 else 17 useradd user$I 18 echo "user$I" | passwd --stdin user$I &> /dev/null 19 fi 20 done elif [ $1 == ‘del‘ ];then 24 for I in {1..10};do 25 if ! id user$I &> /dev/null;then 26 echo "user$I not exists" 27 else 28 userdel user$I 29 echo "user$I deleted" 30 fi 31 done else echo "Unkown you input you common!" 32 fi
-
更加复杂的添加用户命令
#!/bin/bash 2 3 echo $1 4 if [ $1 == ‘--add‘ ];then 5 for I in `echo $2 | sed ‘s/,/ /g‘`;do 6 if id $I &> /dev/null;then 7 echo "$I exists" 8 else 9 useradd $I 10 echo $I | passwd --stdin $I &> /dev/null 11 echo "$I add finish" 12 fi 13 done 14 elif [ $1 == ‘--del‘ ];then for I in `echo $2 | sed ‘s/,/ /g‘`;do 16 if id $I &> /dev/null;then 17 userdel -r $I 18 echo "$I deleted" 19 else 20 echo "$I not exist" 21 fi 22 done 23 elif [ $1 == ‘--help‘ ];then 24 echo "Usage:--add,--del" 25 else 26 echo "Unkown options" 27 fi
-
计算100内的奇数和,偶数和
declare -i ODDSUM declare -i EVENSUM for i in `seq 1 100`;do if [ $[$i%2] == 0 ];then let EVENSUM+=$i else let ODDSUM+=$i fi done echo "ODDSUM=$ODDSUM" echo "EVENSUM=$EVENSUM"
-
更为强大的添加删除帮助
#!/bin/bash # DEBUG=0 ADD=0 DEL=0 for I in `seq 0 $#`; do if [ $# -gt 0 ]; then case $1 in -v|--verbose) DEBUG=1 shift ;; -h|--help) echo "Usage: `basename $0` --add USER_LIST --del USER_LIST -v|--verbose -h|--help" exit 0 ;; --add) ADD=1 ADDUSERS=$2 shift 2 ;; --del) DEL=1 DELUSERS=$2 shift 2 ;; *) echo "Usage: `basename $0` --add USER_LIST --del USER_LIST -v|--verbose -h|--help" exit 7 ;; esac fi done if [ $ADD -eq 1 ]; then for USER in `echo $ADDUSERS | sed ‘[email protected],@ @g‘`; do if id $USER &> /dev/null; then [ $DEBUG -eq 1 ] && echo "$USER exists." else useradd $USER [ $DEBUG -eq 1 ] && echo "Add user $USER finished." fi done fi if [ $DEL -eq 1 ]; then for USER in `echo $DELUSERS | sed ‘[email protected],@ @g‘`; do if id $USER &> /dev/null; then userdel -r $USER [ $DEBUG -eq 1 ] && echo "Delete $USER finished." else [ $DEBUG -eq 1 ] && echo "$USER not exist." fi done fi
-
写一个脚本showlogged.sh,其用法格式为:showlogged.sh -v -c -h|--help其中,-h选项只能单独使用,用于显示帮助信息;-c选项时,显示当前系统上登录的所有用户数;如果同时使用了-v选项,则既显示同时登录的用户数,又显示登录的用户的相关信息;
#!/bin/bash # declare -i SHOWNUM=0 declare -i SHOWUSERS=0 for I in `seq 1 $#`; do if [ $# -gt 0 ]; then case $1 in -h|--help) echo "Usage: `basename $0` -h|--help -c|--count -v|--verbose" exit 0 ;; -v|--verbose) let SHOWUSERS=1 shift ;; -c|--count) let SHOWNUM=1 shift ;; *) echo "Usage: `basename $0` -h|--help -c|--count -v|--verbose" exit 8 ;; esac fi done if [ $SHOWNUM -eq 1 ]; then echo "Logged users: `who | wc -l`." if [ $SHOWUSERS -eq 1 ]; then echo "They are:" who fi fi
-
用户输入文件,按照指定压缩格式进行压缩
#!/bin/bash read -p "input three file to tar :" FILE1 FILE2 FILE3 read -p "select object name:" TARNAME read -p "select Compress type:gzip|bzip2|xz:" COMPRESS case $COMPRESS in gzip) tar -zcf ${TARNAME}.tar.gz $FILE1 $FILE2 $FILE3 ;; bzip2) tar -jcf ${TARNAME}.tar.bz2 $FILE1 $FILE2 $FILE3 ;; xz) tar -Jcf ${TARNAME}.tar.xz $FILE1 $FILE2 $FILE3 ;; *) echo "Unknow you command" ;; esac
-
写一个脚本:1) 显示一个菜单给用户:d|D) show disk usages.m|M) show memory usages.s|S) show swap usages.*) quit.2) 当用户给定选项后显示相应的内容;
#!/bin/bash # cat << EOF d|D) show disk usages. m|M) show memory usages. s|S) show swap usages. *) quit. EOF read -p "Your choice: " CHOICE while [ $CHOICE != ‘quit‘ ];do case $CHOICE in d|D) echo "Disk usage: " df -Ph ;; m|M) echo "Memory usage: " free -m | grep "Mem" ;; s|S) echo "Swap usage: " free -m | grep "Swap" ;; *) echo "Unknown.." ;; esac read -p "Again, your choice: " CHOICE done
-
写一个脚本:1) 显示一个菜单给用户:d|D) show disk usages.m|M) show memory usages.s|S) show swap usages.*) quit.2) 当用户给定选项后显示相应的内容;当用户选择完成,显示相应信息后,不退出;而让用户再一次选择,再次显示相应内容;除了用户使用quit;
#!/bin/bash cat << EOF d|D) show disk usages. m|M) show memory usages. s|S) show swap usages. *) quit. EOF echo -ne "\033[1;34mplease input you choice(input quit to exit):\033[0m" read CHOICE while [ $CHOICE != "quit" ];do case $CHOICE in d|D) echo -e "\e[1;32mdisk usages\e[0m" df -Ph ;; m|M) echo -e "\e[1;32mmemory usages\e[0m" free -m | grep "Mem" ;; s|S) echo -e "\e[1;32mswap usages\e[0m" free -m | grep "Swap" ;; *) echo "Unkown you input command" ;; esac echo -e "\e[1;35mplease input againt command:\e[0m" read CHOICE done
-
写一个脚本(前提:请为虚拟机新增一块硬盘,假设它为/dev/sdb),为指定的硬盘创建分区:1、列出当前系统上所有的磁盘,让用户选择,如果选择quit则退出脚本;如果用户选择错误,就让用户重新选择;2、当用户选择后,提醒用户确认接下来的操作可能会损坏数据,并请用户确认;如果用户选择y就继续,n就退出;否则,让用户重新选择;3、抹除那块硬盘上的所有分区(提示,抹除所有分区后执行sync命令,并让脚本睡眠3秒钟后再分区);并为其创建三个主分区,第一个为20M,第二个为512M, 第三个为128M,且第三个为swap分区类型;(提示:将分区命令通过echo传送给fdisk即可实现)
#!/bin/bash echo "Initial a disk...." echo -e "\033[31mWarning:\033[0m" fdisk -l 2> /dev/null | grep -o "^Disk /dev/[sh]d[a-z]" read -p "your chioce:" PARTDISK if [ $PARTDISK == ‘quit‘ ];then echo "quit" exit 7 fi until fdisk -l 2> /dev/null | grep -o "^Disk /dev/[sh]d[a-z]" | grep "^Disk $PARTDISK" &> /dev/null;do read -p "Wrong option,your choice aging:" PARTDISK done read -p "Will destroy all data,continue,are you sure[y|n]: " CHIOCE until [ $CHIOCE == ‘y‘ -o $CHIOCE == ‘n‘ ];do read -p "Will destroy all data,continue: " CHIOCE done if [ $CHIOCE == ‘n‘ ];then echo "Quit" exit 9 else dd if=/dev/zero of=$PARTDISK bs=512 count=1 sync sleep 3 echo ‘n p 1 +20M n p 2 +512M n p 3 +128M t 3 82 w‘| fdisk $PARTDISK &> /dev/null partprobe $PARTDISK sync sleep 2 mke2fs -j ${PARTDISK}1 &> /dev/null mke2fs -j ${PARTDISK}2 &> /dev/null mkswap ${PARTDISK}3 &> /dev/null
-
复制二进制程序及其依赖的库文件的脚本:
#!/bin/bash # DEST=/mnt/sysroot libcp() { LIBPATH=${1%/*} [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished." } bincp() { CMDPATH=${1%/*} [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH [ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do libcp $LIB done } read -p "Your command: " CMD until [ $CMD == ‘q‘ ]; do ! which $CMD &> /dev/null && echo "Wrong command" && read -p "Input again:" CMD && continue COMMAND=` which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"` bincp $COMMAND echo "copy $COMMAND finished." read -p "Continue: " CMD done