处理用户输入

demo1
简单的读取单个参数

#!/bin/bash
#ussing one command line parameter

factorial=1
for (( number = 1; number <= $1; number++ ))
do
    factorial=$[ $factorial * $number ]
done
echo the factorial of $1 is $factorial

//
hadoop@master:~/shell_test/cin$ ./test1 3
the factorial of 3 is 6
hadoop@master:~/shell_test/cin$ ./test1 4
the factorial of 4 is 24

注意:
如果有多个参数,依次为$1 - $9,如果超过9个
如10,则为${10}

demo2
读取程序名字

#!/bin/bash
#testting $0 parameter

echo The command enter is: $0

#not include path
name=`basename $0`

echo the commadn name is $name

//result
hadoop@master:~/shell_test/cin$ ./test2
The command enter is: ./test2
the commadn name is test2

demo3
测试参数是否存在

#!/bin/bash
#testing parameter before use

if [ -n "$1" ]
then
    echo Hello $1 gland to meet you
else
    echo "sorry, you dont identify yourself."
fi
//result
hadoop@master:~/shell_test/cin$ ./test3
sorry, you dont identify yourself.
hadoop@master:~/shell_test/cin$ ./test3 a
Hello a gland to meet you

特殊参数变量

(1)$#
$# 统计命令行参数个数
if [ $# -ne 2 ]
then
fi
注意在字符串中直接输出需要,${!#}
(2) $* $@
$* 会将所有参数当成一个参数
$@ 变量会单独处理每个参数

demo4
shift移动变量

#!/bin/bash
# demonstrating the shift command

count=1
while [ -n "$1" ]
do
    echo  "Parameter #$count = $1"
    count=$[ $count +1 ]
    shift
done

//result
hadoop@master:~/shell_test/cin$ ./test4 kfd fjdslf
Parameter #1 = kfd
Parameter #2 = fjdslf
注意:
shift 2 移动两个参数

demo5
(1)处理简单选项

#!/bin/bash
#extracting command line options as parameters

while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Fond the -a option" ;;
    -b) echo "Fond the -b option" ;;
    -c) echo "Found the -c option" ;;
    *) echo "$1 is not an option" ;;
    esac
    shift
done
//
hadoop@master:~/shell_test/cin$ ./test5 -c
Found the -c option
hadoop@master:~/shell_test/cin$ ./test5 -b
Fond the -b option
hadoop@master:~/shell_test/cin$ ./test5 -d
-d is not an option

(2)分离参数和选项

#!/bin/bash
#extracting command line options as parameters
#-- as a seperate 

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

count=1
for parm in $@
do
    echo "Parametrer #$count : $parm"
    count=$[ $count + 1 ]
done

//
hadoop@master:~/shell_test/cin$ ./test6 -a -b -c -- fjd fdsjfld jflds
Fond the -a option
Fond the -b option
Found the -c option
Parametrer #1 : fjd
Parametrer #2 : fdsjfld
Parametrer #3 : jflds
hadoop@master:~/shell_test/cin$ 

(3)处理带值的选项

#!/bin/bash
#extracting command line options as parameters
#-- as a seperate 

while [ -n "$1" ]
do
    case "$1" in
    -a) echo "Fond the -a option" ;;
    -b) param=$2
        echo "Found the -b option , with paramerter value $param"
        shift  ;;
    -c) echo "Found the -c option" ;;
    --) shift
        break ;;
    *) echo "$1 is not an option" ;;
    esac
    shift
done

count=1
for parm in $@
do
    echo "Parametrer #$count : $parm"
    count=$[ $count + 1 ]
done
//result
hadoop@master:~/shell_test/cin$ ./test7 -a -b nihao -c
Fond the -a option
Found the -b option , with paramerter value nihao
Found the -c option
hadoop@master:~/shell_test/cin$ 

demo6
获取用户输入

#!/bin/bash
#testing the read command

echo -n "Enter your name"
read name
echo "Heollo $name , welcome to my program"

//
hadoop@master:~/shell_test/cin$ ./test8
Enter your namechenhanghang
Heollo chenhanghang , welcome to my program
注意:
(1)-p 直接指定提示符号
read -p "input your name" name
(2)如果不指定变量名字,则会把变量放在 REPLY中
(3)-s 以隐藏方式读取

demo7
超时退出

#!/bin/bash
#timing the data entry

if read -t 5 -p "please input your name:" name
then
    echo "Hello $name, welcome to my script"
else
    echo
    echo "Sorry, too slow"
fi

//
hadoop@master:~/shell_test/cin$ ./test9
please input your name:
Sorry, too slow
hadoop@master:~/shell_test/cin$

demo9
限定字符的个数

#!/bin/bash
#getting just one character of input

read -n1 -p "Do you want to continue [Y/N] " answer
case $answer in
Y | y) echo
    echo "fine ,continue on..." ;;
N | n) echo
    echo "OK googbye"
    exit ;;
esac
echo "This is the end of the script"

//result
hadoop@master:~/shell_test/cin$ ./test10
Do you want to continue [Y/N] Y
fine ,continue on...
This is the end of the script
hadoop@master:~/shell_test/cin$ 
hadoop@master:~/shell_test/cin$ ./test10
Do you want to continue [Y/N] N
OK googbye
hadoop@master:~/shell_test/cin$ 

demo10

#!/bin/bash
#reading data from file

count=1

cat q | while read line
do
    echo "Line $count: $line"
    count=$[ $count +1 ]
done
echo Finish processing the data

//result
hadoop@master:~/shell_test/cin$ ./test11
Line 1: #!/bin/bash
Line 2: #getting just one character of input
Line 3: 
Line 4: read -n1 -p "Do you want to continue [Y/N] " answer
Line 5: case $answer in
Line 6: Y | y) echo
Line 7: echo "fine ,continue on..." ;;
Line 8: N | n) echo
Line 9: echo "OK googbye"
Line 10: exit ;;
Line 11: esac
Line 12: echo "This is the end of the script"
Line 13: 
Finish processing the data
hadoop@master:~/shell_test/cin$ 

demo 12
getopts 工具获取命令行参数

#!/bin/bash
#processing options and parameters with getopts

while getopts :ab:cd opt
do
    case "$opt" in
    a) echo "Found the -a option" ;;
    b) echo "Found the -b option with the value $OPTARG" ;;
    c) echo "Found the -c option" ;;
    d) echo "Found the -d option" ;;
    *) echo "Unhnown option: $opt" ;;
    esac
done
shift $[ $OPTIND -1 ]

count=1
for param in "$@"
do
    echo "Parameter $count: $param"
    count=$[ $count + 1 ]
done

adoop@master:~/shell_test/cin$ ./test12 -a -b test1 -d test2 test3 test4
Found the -a option
Found the -b option with the value test1
Found the -d option
Parameter 1: test2
Parameter 2: test3
Parameter 3: test4
hadoop@master:~/shell_test/cin$ ./test12 -a -b  -d test2 test3 test4
Found the -a option
Found the -b option with the value -d
Parameter 1: test2
Parameter 2: test3
Parameter 3: test4
hadoop@master:~/shell_test/cin$ 

你可能感兴趣的:(处理用户输入)