Write a script so that the parameters can be computed regardless of the number of arguments followed by the script:
#!/bin/bash
Sum=0
For i in ' seq 1 $# ';d o
Let sum+=$1
Shift
Done
Echo $sum
Write a script that uses the following form
Userinfo.sh-u username [-v{1|2}]
The-u option is used to specify the user, and the script displays the user's UID and GID;
If you use the-V option at the same time:
If the value after-V is 1, the user's home directory path is displayed additionally
If the value after-V is 2, the user's home directory path and Shell are displayed extra
#!/bin/bash
#
[$#-lt 2] && echo "Too less arguments, quit." && exit 3
#上面一句是当参数不足2个的时候, Force quit
if [["$" = = "-U"]];then
Username= "$"
Shift 2
Fi
#上段功能是确认参数-U, write the following arguments to the variable username, and then turn the first two parameters out
If [$#-ge 2] && ["$" = = "-V"];then
Verflag=$2
Fi
#上段功能是当轮替后参数等于2, and the first parameter equals-V, the second argument is written to the Verflag variable
VERFLAG=${VERFLAG:-0}
#默认变量verFlag为0
If [-N $verFlag];then #如果变量verFlag不为空
if! [[$verFlag =~ [012]]];then #如果变量verFlag不属于012中任何一个
echo "wrong parameter."
echo "Usage: ' basename '-u username-v {1|2}"
Exit 4
Fi
Fi
# echo $userName $verFlag This sentence is for testing.
If [$verFlag-eq 1];then
grep "^ $userName"/etc/passwd | Cut-d:-f1,3,4,6
elif [$verFlag-eq 2];then
grep "^ $userName"/etc/passwd | Cut-d:-f1,3,4,6,7
Else
grep "^ $userName"/etc/passwd | Cut-d:-f1,3,4
Fi
The final question is a bit of difficulty, the feeling belongs to the horse's impromptu play, after all, did not draw a flowchart.
2014 Ma linux0218-4 shift rotation and the last big question