1、編寫腳本/root/bin/createuser.sh威根,實現(xiàn)如下功能:使用一個用戶名做為參數缅茉,如果指定參數的用戶存在嘴脾,就顯示其存在,否則添加之蔬墩;顯示添加的用戶的id號等信息
#!/bin/bash
useradd $1 &> /dev/null
if [ $? -eq 0 ] ;then
echo "user $1 is created"
id $1
else
echo "user exist already or no argument "
fi
2译打、編寫腳本/root/bin/yesorno.sh,提示用戶輸入yes或no,并判斷用戶輸入的是yes還是no,或是其它信息
#!/bin/bash
read -p "please input yes or no: " ans
case $ans in
[Yy]|[Yy][Ee][Ss])
echo "your answer is yes"
;;
[Nn]|[Nn][Oo])
echo "your answer is no"
;;
*)
echo "Error,please input again"
;;
esac
3拇颅、編寫腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑奏司,顯示其文件類型(普通,目錄樟插,鏈接韵洋,其它文件類型)
#!/bin/bash
if [[ $# -lt 1 ]] ;then
echo -e "Error: No argument.\n\tUsage: $0 FILENAME"
exit 1
else
if [[ -e $1 ]] ;then
FileType=`ls -ld $1 | cut -c1`
case $FileType in
l)
echo "$1 is a link file"
;;
d)
echo "$1 is a diretory"
;;
-)
echo "$1 is a common file"
;;
*)
echo "$1 is other file"
;;
esac
else
echo "$1: no such file or diretory."
fi
fi
unset FileType
4、編寫腳本/root/bin/checkint.sh,判斷用戶輸入的參數是否為正整數
#!/bin/bash
read -p "請入一個數字: " num
[ -z "$num" ] && echo "請輸入一個數字" && exit 1
NUM=$(echo $num | egrep -o "\-?[[:digit:]]+")
if [ "$NUM" == "$num" ];then
if [ "$NUM" -lt "0" ];then
echo "您輸入的是一個負整數"
elif [ "$NUM" -eq "0" ];then
echo "您輸入的是一個0"
else
echo "您輸入的是一個正整數"
fi
else
echo "您輸入的不是一個整數,請重新運行腳本"
fi
unset NUM