Linux Handling User Input

内容概要: command line parameters, command line options, reading input directly from the keyboard…

1. Command Line Parameters(Data values added after the command):

1.1 After the 9th parameter, you must use braces around the variable number, such as ${10}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash

#command name
echo \$0 = $0

#number of command
echo \$# = $#

#Grabbing all the data
echo \$* = $*
echo \$@ = $@


echo \$1 = $1
echo \$2 = $2
echo \$3 = $3
echo \$4 = $4
echo \$5 = $5
echo \$6 = $6
echo \$7 = $7
echo \$8 = $8
echo \$9 = $9

#大于9之后, 必须使用${}才能得到正确的值
echo \$10 = ${10}
echo \$11 = ${11}
echo \$12 = ${12}
echo \$21 = ${21}
echo \$22 = ${22}
echo \$13 = ${13}
echo \$14 = ${14}
echo \$15 = ${15}
echo \$16 = ${16}
echo \$17 = ${17}
echo \$18 = ${18}
echo \$19 = ${19}
echo \$20 = ${20}
echo \$21 = ${21}
echo \$22 = ${22}
1.2 Test Parameters:

It’s always a good idea to check your parameters to make sure there’s really data.

1
2
3
4
5
6
7
8
#!/bin/bash

if [ -n "$1" ]
then
echo You have some parameters : $1, congratulatons!
else
echo "Sorry, you don't have any parameters!"
fi

1.3 Special Parameter Variables : $0, $
  • counting parameter : $#
  • grabbing all the data : $*, $@
    1
    2
    3
    4
    5
    6
    7
    #!/bin/bash

    #command name
    echo \$0 = $0

    #number of command
    echo \$# = $#
1.4 Special Parameter Variables : $*, $@
The $* variable treated all of the parameters as a single word, while the $@ variable treated each parameter separately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#Grabbing all the data
echo \$* = $*
echo \$@ = $@


count=1
for param in "$*"
do
echo "\$* Parameter #$count = $param"
count=$[$count+1]
done


count=1
for param in "$@"
do
echo "\$@ Parameter #$count = $param"
count=$[$count+1]
done
1.5 shift command:

This is another great way to iterate through command line parameters. The shift command does what it says, it shifts the command line parameters in their relative positions.

1
2
3
4
5
6
7
#!/bin/bash

while [ -n "$1" ]
do
echo "Next Parameter : $1"
shift
done

或者指定shift参数:

1
2
3
4
5
6
7
8
9
#!/bin/bash

echo "The original parameters : $@"

#Be careful when working with the shift command. When a parameter is shifted out, its value is lost and can't be recovered.
shift 2
echo "The new parameter \$@ = $@"
echo "The new parameter \$* = $@"
echo "The new first parameter : $1"


2. Command Line Options(Single-letter values that modify the behaviour of the command):

Options are single letters preceded by a dash that alter the behavior of a command. The following section shows 3 different methods for working with options in your shell scripts.

2.1 Finding your options
2.1.1 Processing simple options
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) echo "Found the -b option";;
-c) echo "Found the -c option";;
*) echo "$1 is not a option";;
esac
#Iterate through the parameters
shift
done
2.1.2 Separating options from parameters

Often you will run into situations where you want to use both options and parameters.For linux, the special character to seperate options and parameters is double dash(–).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

while [ -n "$1" ]
do
case "$1" in
-a) echo "-a option";;
-b) echo "-b option";;
-c) echo "-c option";;
--) shift; break;;
*) echo "$1 is not a option";;
esac
shift
done

count=1
for param in $@
do
echo "Next parameter : $1"
shift
done

2.1.3 Processing options with values

some options require an additional parameter value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

while [ -n "$1" ]
do
case "$1" in
-a) echo "-a option";;
-b) param="$2"
echo "-b option with parameter value: $param"
shift 2;;
-c) echo "-c option";;
*) echo "$1 is not an option";;
esac
shift
done


2.2 getopt & getopts:
  • Firstly, list each command line option letter you are going to use in your script in the optstring
  • Then, place a colon(:) after each option letter that requires a parameter value.
    getopt
    命令格式 : getopt options optstring parameters
    1
    2
    3
    #
    $ getopt ab:c:d -a -b paramb -c paramc -d addtionalparam
    -a -b paramb -c paramc -d -- addtionalparam
getopts

命令格式 : getopts optstring variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

while getopts :ab:c:d opt
do
case "$opt" in
a) echo "-a option";;
b) echo "-b option with parameter value : $OPTARG";;
a) echo "-c option with parameter value : $OPTARG";;
a) echo "-d option";;
*) echo "unknown option : $opt";;
esac
done

shift $[$OPTIND-1]

count=1
for param in "$@"
do
echo "Next Parameter : $1"
shift
done

执行结果

1
2
3
4
5
6
7
$ bash ttt -a -b zhan -c fangm -d hell wowe 
-a option
-b option with parameter value : zhan
unknown option : c
unknown option : d
Next Parameter : hell
Next Parameter : wowe

3. Reading input directly from the keyboard:

The bash shell provides the read command to make your scripts interactive.

3.1 read & read -p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

echo -n "Enter your name: "
read name
echo "Your name is $name"

#(read -p) allows you to specify a prompt directly in the read command line
read -p "Please enter your age: " age
days=$[$age*365]
echo "That makes you over $days days old"


read -p "Enter your first & last name : " first last
echo "Checking data for $last.$first"

#####3.2 Timing out : read -t
Use -t option to set a timer that the number of seconds for the read command to wait for input.

1
2
3
4
5
6
7
8
9
#!/bin/bash

if read -t 5 -p "Please enter your name: " name
then
echo "Hello $name, welcome!"
else
echo
echo "Sorry, too slow"
fi

3.3 read -n2 -p

Instead of timeing the input, you can also set the read command to count the input characters. When a preset number of characters has been entered, it automatically exits, and assigning the data to the variable.

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash

read -n3 -p "Do you want to continue[YES/NO]?" answer
case $answer in
YES|yes) echo
echo "Fine, continue on...";;
NO|no) echo
echo "OK, Goodbye!"
exit;;
esac
echo "This is the end of the script!"

3.4 Silent reading : read -s

Sometimes you need to input from the script user, but you don’t want that input to display on the monitor.

1
2
3
4
5
#!/bin/bash

read -s -p "Enter your password : " pass
echo
#echo "Is your password really $pass?"

3.5 Reading from a file:

Each call to the read command reads a single line of text from the file.

1
2
3
4
5
6
7
8
9
#!/bin/bash

count=1
/bin/cat LinuxHandlingUserInput.md | while read line
do
echo "Line $count : $line"
count=$[$count+1]
done
echo "Finished processing the file!"