2014 Ma linux0218-4 shift rotation and the last big question

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.