Linux&shellの処理ユーザー入力

13099 ワード

前に書きます:ケース、よく使う、分類して、説明します.(By Jim)
コマンドラインパラメータ$1は1番目のパラメータ、$2は2番目のパラメータです.例:
#!/bin/bash
# using one command line parameter

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

呼び出し./test 1 5(これでパラメータを渡した)結果:The factorial of 5 is 120
#!/bin/bash
# testing parameters before use

if [ -n "$1" ]
then
  echo Hello $1,glad to meet you.
else
  echo "Sorry,you didn't identify yourself."
fi

(パラメータが入力されているかどうかを検証することが望ましい.これが最も安全で、プログラミング時にもこのように処理する)すべてのデータ$*すべてのパラメータを1つの単語として$@すべてのパラメータを複数の単語として$#最後のパラメータとして取得する
#!/bin/bash
# testing $* and $@

echo "Using the \$* method:$*"
echo "Using the \$@ method:$@"

./test1 rich jessica tom

結果:Using the$*method:rich jessica tomUsing the$@method:rich jessica tom差はないようですが、一例を見てみると差がわかります
#!/bin/bash
# testing $* and $@

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

テスト:test 1 1 1 2 3 4 5結果:$*Parameter#1=1 2 3 4 5$@Parameter#1=1$@Parameter#2=2$@Parameter#3=3$@Parameter#4=4$@Parameter#5=5(結果から差がわかる)シフトshiftはパラメータ変数を1つ左にシフトし、変数$3の値は変数$2にシフトし、変数$2の値は変数$1にシフトし、変数$1の値は破棄される.
#!/bin/bash
# demonstrating the shift command

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

実行:test 1 1 1 2 3 4 5結果:Parameter#1=1 Parameter#2=2 Parameter#3=3 Parameter#4=4 Parameter#5=5(1桁前に移動するたびに最後に空になった)shift 2は2桁移動処理オプションを示します
#!/bin/bash
# extracting options and parameters

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" ;;
  --) shift
      break;;
  *)  echo "$1 is not an option" ;;
  esac
  shift
done

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

テスト:test 1-a-b-c test 1 test 2-c結果:Found the-a optionFound the-b optionFound the-c optiontest 1 is not an optiontest 2 is not an optionFound the-c optionテスト2:test 1-a-b-c--test 1 test 2-a結果:Found the-a optionFound the-b optionFound the-c optionParameter#1:test 1 Parameter#2:test 2 Parameter#3:-a(ループを飛び出し、残りのパラメータを$@に格納し、以下のコードで使用するため、-aが現れても発見されない)getoptコマンドgetopt-q ab:cd-a-b-test 1-cde test 2 test 3を使用(a,b,c,dを表し、bの後にパラメータが続く)解析の結果は-a-b'-test 1'-c-d-'test 2'''test 3'getopt-q ab:cd-a-b-test 1-d-c-cd test 2-c test 3解析の結果-a-b'-test 1'-d-c-c-c-'test 2''test 3'(すべてのオプションがパラメータから分離され、一定の順序で自動的に追加されます--)完全なコードが表示されます.
 #!/bin/bash
# extracting options and parameters
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
  case "$1" in
  -a) echo "Found the -a option" ;;
  -b) param="$2"
      echo "Found the -b option,with parameter value $param"
      shift;;
  -c) echo "Found the -c option" ;;
  --) shift
      break;;
  *)  echo "$1 is not an option" ;;
  esac
  shift
done

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

テスト:test 1-a-b test 1-cd test 2 test 3結果:Found the-a optionFound the-b option,with parameter value'test 1'Found the-c optionParameter 1:'test 2'Parameter 2:'test 3'(解析データに従って処理されていると想像できる)getoptsは従兄弟getoptによく似ています.ケース:
#!/bin/bash
# simple demonstration of the getopts command

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

テスト:test 1-ab test 1-c-d結果:Found the-a optionFound the-b option,with parameter value test 1 Found the-c optionUnknown option:?getoptsコマンド各処理オプションでは、環境変数OPTINDの値に1が加算されます.getopts処理の最後に達すると、shiftコマンドとOPTIND値を使用してパラメータに移動できます.コードを見てください:
#!/bin/bash
# simple demonstration of the getopts command

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

shift $[ $OPTIND -1 ]

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

テスト:test 1-a-b test 1-cd test 2 test 3結果:Found the-a optionFound the-b option,with parameter value test 1 Found the-c optionFound the-d optionParameter 1:test 2 Parameter 2:test 3取得ユーザ入力基本読取readコマンド受付標準入力、入力後、readコマンドはデータを1つの標準変数に格納します.ケース:
#!/bin/bash
# testing read

echo "Enter your name:"
read name
echo "Welcome ,$name !"

テスト:test 1 Enter your name:jimWelcome,jim!
#!/bin/bash
# testing read

echo -n "Enter your name:"
read name
echo "Welcome ,$name !"

テスト:[root@localhost shellscript]# test1Enter your name:jimWelcome ,jim !(後の新しい文字の出現を抑制し、-nは改行しません)readには-pコマンドがあり、readコマンドラインに直接ヒントを指定できます.
#!/bin/bash
# testing read -p option

read -p "Please enter your age:" age
days=$[ $age * 360 ]
echo "That makes you over $days days old!"

テスト:[root@localhost shellscript]# test1Please enter your age:25That makes you over 9000 days old!(25は入力)