日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

shell基础05 处理用户输入

發布時間:2023/12/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shell基础05 处理用户输入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.? 命令行參數------類似javac? 參數1 參數2

? ? ?類似Java中編譯的javac parm1....。在shell中,參數與參數之間用空格隔開。采用位置參數來識別對應的參數值:$0是程序名,$1是第一個參數,以此類推,知道第9個參數$9。對于大于9個參數的需要在變量數字周圍添加花括號,比如${10}。

? ? ?note:命令行上不僅可以處理數值,還可以處理字符串。

1 [Hermioner@localhost Documents]$ cat test2.sh 2 #!/bin/bash 3 total=$[ $1*$2 ] 4 echo The first parm is $1 5 echo The second parm is $2 6 a=$3 7 echo the third parm is $3 8 b=$4 9 echo the forth parm is $4 10 [Hermioner@localhost Documents]$ bash test2.sh 2 3 "hello world" min 11 The first parm is 2 12 The second parm is 3 13 the third parm is hello world 14 the forth parm is min View Code 1 [Hermioner@localhost Documents]$ cat test3 2 #!/bin/bash 3 echo the tenth parm is ${10} 4 echo the eleventh parm is ${11} 5 [Hermioner@localhost Documents]$ bash test3 1 2 3 4 5 6 7 8 9 10 11 6 the tenth parm is 10 7 the eleventh parm is 11 8 [Hermioner@localhost Documents]$ View Code

? ? ? note: $0返回腳本名,如果用bash,就只返回腳本名;如果./腳本來運行,返回當前路徑名;? 因此,還可以嘗試basename命令來返回不包含路徑的腳本名。

2. $#,$*,$@,${!#}

? ? s#? ? ? 用來統計命令行的參數個數

? ? s*? ? ? ?用來訪問所有的命令行參數,并且構成一個字符串整體輸出

? ? s@? ? ?同s*,只是結果是分散成字符串數組,每個數組中的元素都是一個參數

? ? ${!#}? ?代表的最后一個參數,因為花括號中不可以用$,因此用!來代替它

1 [Hermioner@localhost Documents]$ cat testfile 2 #!/bin/bash 3 echo the "\$*" is $* 4 echo the "\$@" is $@ 5 echo the "\$#" is $# 6 echo the "\${!#}" is ${!#} 7 8 [Hermioner@localhost Documents]$ bash testfile a b c d 9 the $* is a b c d 10 the $@ is a b c d 11 the $# is 4 12 the ${!#} is d View Code 1 [Hermioner@localhost Documents]$ cat testfile 2 #!/bin/bash 3 echo 4 count=1 5 for param in "$*" 6 do 7 echo "\$* Parameter #$count = $param" 8 count=$[ $count+1 ] 9 done 10 11 echo 12 count=1 13 for param in "$@" 14 do 15 echo "\$@ Parameter #$count = $param" 16 count=$[ $count+1 ] 17 done 18 19 [Hermioner@localhost Documents]$ bash testfile a b c d 20 21 $* Parameter #1 = a b c d 22 23 $@ Parameter #1 = a 24 $@ Parameter #2 = b 25 $@ Parameter #3 = c 26 $@ Parameter #4 = d 27 [Hermioner@localhost Documents]$ View Code

3. shift 移動變量??

? ? ?shift可以用來在不知道有多少參數,以及每個參數的值的情況下進行遍歷,因為它始終可以只打印第一個值。默認情況下它會將每個參數變量向左移動一個位置。所以變量$3的值會移動到$2中,$2的值會移動到$1中,而變量$1的值則會被刪除(note:$0代表程序嗎,不會改變)

? ? ?也可以shift n 來指定左移動多少個,eg: shift 2? ?,則$3的會移動到$1中,這樣就可以跳過一些值不遍歷了。

1 [Hermioner@localhost Documents]$ cat test3.sh 2 #!/bin/bash 3 echo "the original parameters is $*" 4 shift 2 5 echo "the new first parameter is $1" 6 [Hermioner@localhost Documents]$ bash test3.sh 1 2 3 4 5 7 the original parameters is 1 2 3 4 5 8 the new first parameter is 3 9 [Hermioner@localhost Documents]$ View Code

? ? ?note:配合shift的使用,同樣可以通過shell腳本中的邏輯來判斷是選項還是參數,從而讓參數得到應有的輸出。并在在bash shell中還提供了getopt和getopts來判斷是選項還是參數-------用時參考它們用法即可。

4. 獲取用戶輸入-------交互性更強,類似java中的scanner+system.in用法

? ? 采用read命令。read后面跟變量名,就可以將輸入的值保存到變量中;如果不輸入變量名,那么就自動保存在了特殊環境變量REPLY中。

1 [Hermioner@localhost Documents]$ cat test1 2 #!bin/bash 3 echo -n "Enter your name:" 4 read name 5 echo "hello $name" 6 [Hermioner@localhost Documents]$ bash test1 7 Enter your name:Tom 8 hello Tom 9 [Hermioner@localhost Documents]$ View Code

? ? ? note1:如果用戶一直不輸入,read會一直等待,因此可以設置計時器,用-t選項。時間過了,就不等了。

? ? ? eg:read -t 5 name

? ? ? note2: 類似密碼輸入,隱藏方式讀取,只需要添加 -s就可以

? ? ? note3: 還可以從文件中讀取,一行一行的讀取

1 [Hermioner@localhost Documents]$ cat test1 2 #!bin/bash 3 a 4 b 5 c 6 [Hermioner@localhost Documents]$ cat test2 7 #!/bin/bash 8 cat test1 | while read line #采用了管道 9 do 10 echo "the line is $line" 11 done 12 echo "read is done" 13 [Hermioner@localhost Documents]$ bash test2 14 the line is #!bin/bash 15 the line is a 16 the line is b 17 the line is c 18 read is done 19 [Hermioner@localhost Documents]$ View Code

補充管道:

? ? ?command1 | command2? ?就是將命令1的輸出重定向到了command2中。 可以多級重定向,多添加|就好了。

?

參考文獻

Linux命令行與shell腳本編程大全(第3版)[美]?布魯姆(Richard Blum),布雷斯納漢(Christine Bresnahan) 著,門佳,武海峰?譯

?

轉載于:https://www.cnblogs.com/Hermioner/p/9383629.html

總結

以上是生活随笔為你收集整理的shell基础05 处理用户输入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 天堂免费在线视频 | 国产精品粉嫩 | 日韩欧美不卡视频 | www国产91 | 日韩国产欧美一区二区三区 | 在线va| 亚洲无毛| 国产经典久久 | av色图| 亚洲免费国产 | 懂色中文一区二区在线播放 | 日韩一级理论片 | 日本激情一区二区 | 国产精品免费久久 | 99热激情| 蜜桃av中文字幕 | 99视频一区二区 | 天天激情| 国产无精乱码一区二区三区 | 国产suv一区二区 | 茄子av在线| 一级性视频 | 日韩不卡在线视频 | 免费黄色av电影 | 色综合一区二区三区 | 久一在线 | 蜜桃免费av| 日本一区二区在线不卡 | 国产高清精品在线观看 | 欧美无遮挡高潮床戏 | 国产资源免费 | 欧美激情亚洲 | 一区二区视频国产 | 国产一区二区三区18 | 国产精品99久久久久久人 | 成人黄色视屏 | 韩国三级中文字幕hd久久精品 | 国产精品久久久久久亚洲影视 | 佐山爱在线视频 | 91精品国产自产精品男人的天堂 | 国产精品久久久久久久9999 | 日韩少妇一区 | 日韩经典中文字幕 | 中文字幕久久久久久久 | 永久av网站| 六月丁香婷婷综合 | 自拍天堂| 丁香六月天婷婷 | 午夜视频黄 | 日韩欧美高清 | 国产成人啪精品午夜在线观看 | 想要视频在线观看 | 叼嘿视频在线免费观看 | 亚洲一区av在线 | 伊人久久大香线蕉av色婷婷色 | 日本国产一区二区 | 男生插女生视频在线观看 | 亚洲成人午夜电影 | 欧美无砖砖区免费 | 久久精品美女视频 | 韩国三级中文字幕 | 久草网在线视频 | 亚洲成av | 欧美伊人网 | 色婷婷av一区二区三区在线观看 | 五月天综合婷婷 | 中文字幕精品无码一区二区 | 色猫咪av在线 | 蜜臀av一区二区 | 国产精品国产三级国产专区51区 | 久久人人爽爽 | 美女又爽又黄又免费 | 欧美一区二区三区 | 亚洲色成人www永久在线观看 | 各处沟厕大尺度偷拍女厕嘘嘘 | 国产伦精品一区二区三区照片 | 色xxxxxx| 午夜激情视频网站 | 一区二区传媒有限公司 | 久久精品欧美 | 精品一区二区久久久久久按摩 | 打开免费观看视频在线播放 | 色欲狠狠躁天天躁无码中文字幕 | 公交顶臀绿裙妇女配视频 | 老司机在线免费视频 | 激情五月开心婷婷 | 色校园| 日韩免费视频一区二区 | 色乱码一区二区三区熟女 | www国产无套内射com | 亚洲免费黄色片 | 日本αv | 亚洲毛片一区 | 国产成人免费在线视频 | 中文字幕第315页 | 图片区偷拍区小说区 | 亚洲欧美激情精品一区二区 | 日日舔夜夜操 | 亚洲一二三四在线观看 |