成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Shell日常使用的小技巧

開發(fā) 前端
Shell 腳本在我們?nèi)粘i_發(fā)和學(xué)習(xí)都有舉足輕重的地位,比如看一些開源項(xiàng)目,比如項(xiàng)目中的各式各樣的腳本,對(duì)于促進(jìn)生產(chǎn)力工具有很大幫助!

Shell 腳本在我們?nèi)粘i_發(fā)和學(xué)習(xí)都有舉足輕重的地位,比如看一些開源項(xiàng)目,比如項(xiàng)目中的各式各樣的腳本,對(duì)于促進(jìn)生產(chǎn)力工具有很大幫助!

[[440772]]

1、命令小技巧

1、-x命令進(jìn)行跟蹤調(diào)試執(zhí)行

 

  1. #!/bin/sh 
  2.  
  3. num1=10 
  4. num2=20 
  5. if (($num1 <= $num2)); then 
  6.     echo num1 lesser equal num2 
  7. else 
  8.     echo num1 greater num2 
  9. fi 

執(zhí)行:

 

  1. ➜  note git:(master) ✗ sh -x /Users/fanhaodong/Desktop/project/test.sh 
  2. + num1=10 
  3. + num2=20 
  4. + (( 10 <= 20 )) 
  5. + echo num1 lesser equal num2 
  6. num1 lesser equal num2 

2、-c命令 (執(zhí)行命令參數(shù))

 

  1. ➜  note git:(master) ✗ sh -c << EOF " 
  2. dquote> echo hello world 
  3. dquote> echo hello world2 
  4. dquote> " 
  5. heredoc> EOF 
  6. hello world 
  7. hello world2 

3、使用set變量

 

  1. #!/bin/sh 
  2.  
  3. # -v  Print shell input lines as they are read
  4. # -x  Print commands and their arguments as they are executed. 
  5. # -e  Exit immediately if a command exits with a non-zero status. 
  6. set -ex 
  7.  
  8. echo "hello world" 
  9.  
  10. exit 1 

執(zhí)行

 

  1. ➜  makefile git:(master) ✗ sh ./main.sh 
  2. + echo 'hello world' 
  3. hello world 
  4. + exit 1 
  5. ➜  makefile git:(master) ✗ echo $?      

幫助可以看:sh -c "help set"

2、語法小技巧

1、${}和$適用場(chǎng)景

1)$ 可能有語法歧義,所以一般使用${}

 

  1. #!/bin/sh 
  2. s="my name is s" 
  3. echo 輸出: $sa 
  4. echo 輸出: ${s}a 
  5.  
  6. #輸出: 
  7. #輸出: my name is sa 

2、((cmd))

  • 可以替換[-le ] 命令

 

  1. num1=10 
  2. num2=20 
  3. if (($num1 <= $num2)); then 
  4.     echo num1 lesser equal num2 
  5. fi 

3、cat[>>|>][file]<

如果重定向的操作符是<<-,那么分界符(EOF)所在行的開頭部分的制表符(Tab)都將被去除。這可以解決由于腳本中的自然縮進(jìn)產(chǎn)生的制表符。

 

  1. ➜  test cat > glide.yaml << EOF 
  2. heredoc> name: tom 
  3. heredoc> age: 10 
  4. heredoc> hobby: 
  5. heredoc> - football 
  6. heredoc> EOF 
  7.  
  8. ➜  test cat glide.yaml 
  9. name: tom 
  10. age: 10 
  11. hobby: 
  12. - football 

4、 單引號(hào),雙引號(hào),沒有引號(hào)的區(qū)別

 

  1. #!/bin/sh 
  2.  
  3. name="tom" 
  4.  
  5. f1(){ 
  6. echo "f1 hello world 
  7. my name is $name 
  8.  
  9. f2(){ 
  10. echo 'f2 hello world 
  11. my name is $name 
  12.  
  13. f3(){ 
  14. echo f3 hello world 
  15. my name is $name 
  16.  
  17. f1 
  18. f2 
  19. f3 

輸出

 

  1. ➜  makefile git:(master) ✗ sh ./main.sh  
  2. f1 hello world 
  3. my name is tom 
  4.  
  5. f2 hello world 
  6. my name is $name 
  7.  
  8. f3 hello world 
  9. ./main.sh: line 19: my: command not found 

可以看到

  • 雙引號(hào)會(huì)自動(dòng)對(duì)變量賦值
  • 單引號(hào)不會(huì)對(duì)變量進(jìn)行賦值等操作
  • 不加引號(hào)對(duì)于換行等功能無法實(shí)現(xiàn)!只能使用換行符

 

  1. f3(){ 
  2. echo f3 hello world \ 
  3. my name is $name 

5、特殊變量

  • $0: 當(dāng)前腳本的文件名
  • $n : 傳遞給腳本或函數(shù)的參數(shù)。n 是一個(gè)數(shù)字,表示第幾個(gè)參數(shù)。例如,第一個(gè)參數(shù)是$1,第二個(gè)參數(shù)是$2。
  • $#: 傳遞給腳本或函數(shù)的參數(shù)個(gè)數(shù)。
  • $*: 傳遞給腳本或函數(shù)的所有參數(shù)。
  • $@: 傳遞給腳本或函數(shù)的所有參數(shù)(推薦使用這個(gè)),當(dāng)使用"" 雙引號(hào)引用是$*會(huì)變成字符串而不是數(shù)組
  • $?: 上個(gè)命令的退出狀態(tài),或函數(shù)的返回值。一般情況下,大部分命令執(zhí)行成功后會(huì)返回 0,失敗返回 1。
  • $$: 當(dāng)前Shell進(jìn)程ID。對(duì)于 Shell 腳本,就是這些腳本所在的進(jìn)程ID。

6、[[]]和[]標(biāo)準(zhǔn) 以及基本語法規(guī)范

具體規(guī)范:

 

  1. #!/bin/sh 
  2. name="" 
  3. if [[ -z $name ]]; then 
  4.     echo "is zero" 
  5. fi 

執(zhí)行后發(fā)現(xiàn)

7、/bin/sh 與 /bin/bash 的區(qū)別

/bin/sh 與 /bin/bash 的區(qū)別

3、獲取命令結(jié)果$(cmd)

有兩種寫法,一種是$()這個(gè)并不是所有的shell都支持,但是比較直觀, 另外一種是"``" (它可是適用更多的平臺(tái))

 

  1. #!/bin/sh  
  2. echo `ls -a /Users/fanhaodong/note`  
  3. echo $(ls -a /Users/fanhaodong/note) 

輸出:

 

  1. . .. .DS_Store 1714.jpg docker-rocketmq-cluster gridea-home hexo-home note pdf vuepress-starter  
  2. . .. .DS_Store 1714.jpg docker-rocketmq-cluster gridea-home hexo-home note pdf vuepress-starter 

4、輸入輸出重定向2>&1

使用

程序中經(jīng)常有,標(biāo)準(zhǔn)輸出,但是還有錯(cuò)誤輸出,因此需要合并到一個(gè)流中

其實(shí)Go的程序中正執(zhí)行腳本的時(shí)候可以指定,標(biāo)準(zhǔn)輸出和錯(cuò)誤輸出

 

  1. command := exec.Command(shell, "-c", cmd) 
  2.   command.Stdout = os.Stdout 
  3.   command.Stderr = os.Stderr 

使用的時(shí)候:

  • 默認(rèn)為標(biāo)準(zhǔn)輸出重定向,與 1> 相同
  • 2>&1 意思是把 標(biāo)準(zhǔn)錯(cuò)誤輸出 重定向到 標(biāo)準(zhǔn)輸出.
  • &>file 意思是把標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出 都重定向到文件file中

例如:

  1. command >out.file 2>&1& 

command >out.file是將command的標(biāo)準(zhǔn)輸出重定向到out.file文件,即輸出內(nèi)容不打印到屏幕上,而是輸出到out.file文件中。2>&1 是將標(biāo)準(zhǔn)出錯(cuò)重定向到標(biāo)準(zhǔn)輸出,這里的標(biāo)準(zhǔn)輸出已經(jīng)重定向到了out.file文件,即將標(biāo)準(zhǔn)出錯(cuò)也輸出到out.file文件中。最后一個(gè)&,是讓該命令在后臺(tái)執(zhí)行。

5、If語句

if 其實(shí)就是test 命令

1、格式

1) 換行寫

 

  1. if [ condition ]; then 
  2.      # body 
  3. elif [ condition ]; then 
  4.      # body 
  5. else 
  6.      # body 
  7. fi 

2)非換行寫

  1. if [ -f "/Users/fanhaodong/note/note/Makefile1" ]; then echo 111 ; echo 222 ;elif [ -f "/Users/fanhaodong/note/note/README.md" ]; then echo 333 ; echo 4444 ; else echo 555 ; echo 666 ; fi 

2、結(jié)果獲取/判斷

結(jié)果輸出0 ,表示為真,可以通過$? 來獲取結(jié)果

3、例如調(diào)試條件

  1. ➜  note git:(master) ✗ test "abc"!="def" 
  2. ➜  note git:(master) ✗ echo $? 

4、測(cè)試文件是否存在

  • 如果你要判斷一個(gè)文件是否存在,只需要-e 即可,輸出0 表示文件存在 (不在判斷類型的時(shí)候推薦使用這個(gè))
  • 如果你要判斷一個(gè)文件是否為文件夾,并且判斷是否存在,只需要-d 即可
  • 如果你要判斷一個(gè)文件是否為常規(guī)文件 ,并且判斷是否存在,只需要-f 即可
  • -L filename 如果 filename為符號(hào)鏈接,則為真

 

  1. [root@019066c0cd63 ~]# ls -al 
  2. lrwxrwxrwx 1 root root    5 Mar  1 09:49 c.txt -> a.txt 
  3. [root@019066c0cd63 ~]# [ -L "./c.txt" ] 
  4. [root@019066c0cd63 ~]# echo $? 
  • -r filename 如果 filename可讀,則為真
  • -w filename 如果 filename可寫,則為真
  • -x filename 如果 filename可執(zhí)行,則為真
  • -s filename 如果文件長(zhǎng)度不為0,則為真
  • -h filename 如果文件是軟鏈接,則為真

 

  1. ➜  note git:(master) ✗ [ -f "/Users/fanhaodong/note/note/Makefile" ] 
  2. ➜  note git:(master) ✗ echo $? 
  3. ➜  note git:(master) ✗ [ -f "/Users/fanhaodong/note/note/Makefile1" ] 
  4. ➜  note git:(master) ✗ echo $? 

5、字符串操作

字符串推薦加"" 進(jìn)行定義

1) 判斷字符串是否為空-z (zero)么

 

  1. #!/bin/sh 
  2.  
  3. str="" 
  4. if [ -z "${str}" ]; then 
  5.     echo str is empty 
  6. fi 
  7. # str is empty 

2)判斷兩個(gè)字符串是否相同

 

  1. #!/bin/sh 
  2.  
  3. str1="str" 
  4. str2="str2" 
  5.  
  6. if [ "$str1" = "$str2" ]; then 
  7.     echo str1 is equal str2 
  8. else 
  9.     echo str1 is not equal str2 
  10. fi 
  11.  
  12. # str1 is not equal str2 

4、測(cè)試一個(gè)命令是否存在command -v $#

 

  1. #!/bin/sh 
  2. cmd=go 
  3. if [ `command -v $cmd` ]; then 
  4.    echo $cmd command is exists  
  5. else 
  6.     echo $cmd command not exists  
  7. fi 
  8. # go command is exists 

5、獲取字符串長(zhǎng)度${#var}

 

  1. #!/bin/sh 
  2. str="hello   "   
  3. str1=hello 
  4. echo str 的長(zhǎng)度是 ${#str} 
  5. echo str1 的長(zhǎng)度是 ${#str1} 
  6.  
  7. #str 的長(zhǎng)度是 8 
  8. #str1 的長(zhǎng)度是 5 

6、數(shù)字比較

  • -eq 等于
  • -ne 不等于
  • -gt 大于
  • -ge 大于等于
  • -lt 小于
  • -le 小于等于

 

  1. #!/bin/sh 
  2.  
  3. num1=10 
  4. num2=20 
  5. if (($num1 <= $num2)); then 
  6.     echo num1 lesser equal num2 
  7. fi 
  8.  
  9. if [ $num1 -le $num2 ]; then 
  10.     echo num1 lesser equal num2 
  11. fi 
  12.  
  13. # num1 lesser equal num2 
  14. # num1 lesser equal num2 

7、shell腳本中if判斷'-a' - '-z'含義

 

 

6、for循環(huán)

1、forin; do;;done

 

  1. #!/bin/bash 
  2.  
  3. for item in {1..5}; do echo "$item"; done 
  4.  
  5. [Running] /bin/bash "/Users/fanhaodong/note/note/Linux/shell/file.sh" 

2、for((x=0; x<10; x++));do;; done

  1. for((x=0; x<10; x++));do echo "$x" ;done 

7、awk

1、例子

demo

 

  1. [root@19096dee708b data]# cat demo.txt 
  2. 11 22 
  3. 111 
  4. 22 33 

腳本

 

  1. [root@19096dee708b data]# awk  -F ':' 'BEGIN {print "start"} /:/ {printf "do1 $1=%s $2=%s\n",$1,$2} {print "do2 .."} END {print "e nd"}' demo.txt 
  2. start 
  3. do1 $1=11 $2=22 
  4. do2 .. 
  5. do2 .. 
  6. do1 $1=22 $2=33 
  7. do2 .. 
  8. e nd 

2、格式

  1. awk -arg1 x1 -arg2 x2 'BEGIN {開始執(zhí)行腳本} 條件 {過程1} {過程2} END{循環(huán)體執(zhí)行完后最后執(zhí)行}' 
  1. 其中條件執(zhí)行正則表達(dá)式、判斷等
  2. 還支持一些內(nèi)置函數(shù) , 一些內(nèi)置變量!
  3. 過程中支持if函數(shù)

8、sed

首先gun的sed函數(shù)和mac的set是有些不同的!具體看:

  • https://superuser.com/questions/307165/newlines-in-sed-on-mac-os-x

具體寫在其他文檔上,目前使用的多個(gè)命令也未分享!

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2022-08-18 10:01:35

Jmeter技巧

2015-12-21 10:54:37

Docker云計(jì)算

2021-05-13 12:46:54

GNU ScreenLinux

2009-08-07 10:18:13

Linux反彈CmdLine S技巧

2011-04-26 16:25:42

掃描儀應(yīng)用維護(hù)

2022-04-02 09:56:44

pipPython

2022-11-29 10:42:46

GoFrame技巧腳手架

2015-07-27 09:36:09

storyboard

2022-08-15 15:43:29

Linuxcron

2009-07-19 10:48:53

LinuxWebShell反彈CmdLine She

2011-05-27 10:02:42

Shell

2011-05-07 11:15:39

掃描儀使用技巧

2020-03-18 14:20:25

shellLinux命令

2011-06-17 09:18:56

sudo技巧

2024-03-26 15:21:43

2022-06-27 17:03:58

LibreOffic

2022-01-06 15:21:32

pipPython技巧

2017-09-06 12:42:45

AndroidGradle開發(fā)技巧

2015-06-17 14:18:30

Cryptsetup加密加密U盤

2019-12-04 12:28:24

TOP命令Linux
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 福利精品在线观看 | 免费在线观看一区二区三区 | 免费观看羞羞视频网站 | 欧美视频第三页 | 91人人爽 | 人人色视频| www.日日夜夜 | 91成人在线视频 | 国产一区久久精品 | 青青草网 | 国产欧美日韩一区 | 97日韩精品 | 欧美三级成人理伦 | 蜜桃传媒av | 日日噜噜夜夜爽爽狠狠 | 国产一级电影在线 | 亚洲一区播放 | 午夜精品久久久 | 国产精品久久久久久久久久免费看 | 国产久 | h视频在线播放 | 欧美群妇大交群中文字幕 | 99精品免费久久久久久久久日本 | 久久久久国产精品一区二区 | 成人高潮片免费视频欧美 | 欧洲视频一区 | 午夜免费在线电影 | 国产综合网站 | 亚洲免费一区 | 亚洲 欧美 在线 一区 | 日韩综合在线视频 | 中文字幕成人 | 精品九九 | 91久久夜色精品国产网站 | 国产精品视频999 | 欧美日韩一区二区电影 | 蜜桃在线一区二区三区 | 亚洲一区二区三区在线播放 | 日韩视频一区二区三区 | 久久久精品天堂 | 欧美中文在线 |