Bash Script Parameters Case Summary
Case 1: The command line parameters given two numbers, the output of the larger values;
Method 1: The following
#!/bin/bash
#Name:
#Version:
#Type:
#Date:
#Author
#Email:
If [$#-lt 2];then
echo "The Intergers."
Fi
If [$1-ge $];then
echo "Max is $"
Else
echo "Max is $"
Fi
Method 2: The following
#!/bin/bash
#Name:
#Version:
#Type:
#Date:
#Author
#Email:
Declare-i Max
If [$#-lt 2];then
echo "The Intergers."
Fi
If [$1-ge $];then
Max=$1
Else
Max=$2
Fi
echo "Max Number: $max"
Method 3: The following
#!/bin/bash
#Name:
#Version:
#Type:
#Date:
#Author
#Email:
Declare-i max=$1
If [$#-lt 2];then
echo "The Intergers."
Fi
If [$1-le $];then
Max=$2
Fi
echo "Max Number: $max"
Case 2: Pass two text file paths to the script by command, and calculate the sum of its blank lines:
#!/bin/bash
#Name:
#Version:
#Type:
#Date:
#Author
#Email:
a=$ (grep "^$" $ | wc-l)
b=$ (grep "^$" $ | wc-l)
sum=$[$a + $b]
echo "The sum is: $sum"
Case 3: Pass a user name to the script by parameter, add it when this user does not exist:
Method 1
#!/bin/bash
If [$#-lt 1];then
echo "At least one username"
Exit 2
Fi
if! grep "^$\>"/etc/passwd &>/dev/null;then
Useradd $
echo $ | passwd--stdin $ &>/dev/null
echo "Add a user $ finishd"
Fi
Method 2
#!/bin/bash
If [$#-lt 1];then
echo "At least one username"
Exit 2
Fi
If grep "^$1\>"/etc/passwd &>/dev/null;then
echo "User $ exists."
Else Useradd $
echo $ | passwd--stdin $ &>/dev/null
echo "Add a user $ finishd"
Fi
Case four write a script, pass the command two text file path to the script, calculate the sum of its blank line number:
#!/bin/bash
a=$ (grep "^$" $ | wc-l)
b=$ (grep "^$" $ | wc-l)
sum=$[$a + $b]
echo "The sum is: $sum"
Case five write a script to calculate the sum of the ID number of the 10th user and the 20th user in the/etc/passwd file
#!/bin/bash
id1=$ (cat/etc/passwd | head-10 | tail-1 | cut-d:-F3)
id2=$ (cat/etc/passwd | head-20 | tail-1 | cut-d:-F3)
sum=$[$id 1+ $id 2]
echo "The sum is: $sum"
Case six The sum of the ID of the user user1 and User2 if two users are not added
#!/bin/bash
ID user1 &>/dev/null | | Useradd user1
ID user2 &>/dev/null | | Useradd User2
user1id=$ (Id-u user1)
user2id=$ (id-u user2)
sum=$[$user 1id+ $user 2ID]
echo "The sum ID is: $sum"
Well, write it down today and continue tomorrow.
This article is from the "Li Wenming blog" blog, make sure to keep this source http://liwenming18.blog.51cto.com/11068518/1755033
Bash Script Parameters Case Summary