shell基礎3,shell基礎

來源:互聯網
上載者:User

shell基礎3,shell基礎

cut是一個選取命令:

就是將一段資料經過分析,取出我們想要的。一般來說,選取資訊通常是針對“行”來進行分析的,並不是整篇資訊分析的。

(1)其文法格式為:

cut  [-bn] [file] 或 cut [-c] [file]  或  cut [-df] [file]

使用說明
cut 命令從檔案的每一行剪下位元組、字元和欄位並將這些位元組、字元和欄位寫至標準輸出。
如果不指定 File 參數,cut 命令將讀取標準輸入。必須指定 -b、-c 或 -f 標誌之一。

主要參數
-b :以位元組為單位進行分割。這些位元組位置將忽略多位元組字元邊界,除非也指定了 -n 標誌。
-c :以字元為單位進行分割。
-d :自訂分隔字元,預設為定位字元。
-f :與-d一起使用,指定顯示哪個地區。
-n :取消分割多位元組字元。僅和 -b 標誌一起使用。如果字元的最後一個位元組落在由 -b 標誌的 List 參數指示的範圍之內,該字元將被寫出;否則,該字元將被排除。

cut的使用

Using the cut command with /etc/passwd
For a first cut command example, I’ll use the /etc/passwd file on my Unix system. I use this file because fields in the file are separated by the “:” character, which make it very easy to work with.

Using that file, here’s a Linux cut command that prints the first field (first column) of every line in that file:

cut -f1 -d: /etc/passwd

This cut command can be read as:

Print the first field (-f1)
Fields are delimited by the “:” character (-d:)
Use the /etc/passwd file as input
The output of this command will vary by Unix and Linux systems, but it will be the first field of your /etc/passwd file, which contains the name of the users on your system. This will look something like:

nobodyalvingeorgefred
#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHfor value in dog "cat" 'bird'   #注意 加引號沒發現有什麼特別的do        echo "animal are ${value}s"donefuhui@ubuntu:~/script$ sh sh22.sh animal are dogsanimal are catsanimal are birds

linux 給使用者加sudo許可權
1、用root帳號登入或者su到root。

2、增加sudoers檔案的寫入權限: chmod u+w /etc/sudoers

3、vim /etc/sudoers 找到 root ALL=(ALL) ALL 在這行下邊添加 dituhui ALL=(ALL) ALL (ps:dituhui代表是你要添加sudo許可權的使用者名稱)

4、除去sudoers檔案的寫入權限: chmod u-w /etc/sudoers

passwd下我的賬戶資訊

fuhui:x:1000:1000:FirstLinux,,,:/home/fuhui:/bin/bash

命令:id 功能說明:查看顯示目前登陸賬戶的uid和gid及所屬分組及使用者名稱

fuhui@ubuntu:~/script$ id fuhuiuid=1000(fuhui) gid=1000(fuhui) groups=1000(fuhui),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare)

命令:finger

功能說明:查詢使用者的資訊,通常會顯示系統中某個使用者的使用者名稱、主目錄、停滯時間、登入時間、登入shell等資訊。如果要查詢遠程機上的使用者資訊,需要在使用者名稱後面接“@主機名稱”,採用[使用者名稱@主機名稱]的格式,不過要查詢的網路主機需要運行finger守護進程。

Login: fuhui                            Name: FirstLinuxDirectory: /home/fuhui                  Shell: /bin/bashOn since Sun Jun 21 18:22 (PDT) on pts/12 from 192.168.187.1   7 seconds idleNo mail.No Plan.
#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHusers=$(cut -f1 -d: /etc/passwd)for name in $usersdo        id $name   #注意,直接執行命令語句        finger $namedone

linux seq更為詳細的用法

seq命令的作用就是列印出一串有序的數字,seq(sequence of number).

它主要有以下3個參數構成:

       -f, --format=FORMAT           use printf style floating-point FORMAT (default: %g)

-f 指定列印的格式:
例如:

[root@hao32]# seq -f %05g 2 7 000020000300004000050000600007
 -s, --separator=STRING          use STRING to separate numbers (default: \n)

-s 指定分隔字元 預設是斷行符號(-s後面可以不帶引號):
例如:

[root@hao32]# seq -s" " 2 7     2 3 4 5 6 7
   -w, --equal-width          equalize width by padding with leading zeroes

-w 輸出是同寬 前面不足的用 “0” 補全,即與位元最多的數對齊
例如:

[root@hao32]# seq -w 2 1102030405060708091011

/dev/null 2>&1 的作用

首先需要瞭解unix系統的輸入輸出資料流,他們分別與數位對應關係是:

    0:標準輸入資料流( stdin )    1 : 標準輸出資料流( stdout )    2 : 標準錯誤流( stderr )

所以 2>&1 表示的意思是將 stderr 重新導向到 stdout, 並一起在螢幕上顯示。如果不加數字,預設的重新導向是針對 stdout 的。

語句:/dev/null 代表空裝置檔案; >代表重新導向; 2代表標準錯誤流; &表示引用,等同的意思。所以語句的意思是,將標準輸出以及標準錯誤輸出重新導向到空裝置檔案,將資訊屏蔽不顯示。

#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHnetwork='192.168.1'for sitenu in $(seq 1 10)do      #注意${sitenu}和$${sitenu}是完全不相同的        ping -c 1 -w 1 "${network}.${sitenu}" > /dev/null && result=0 || result=1  #注意 > /dev/null,沒有找到-w的用處        if [ "$result" = 0 ]; then                echo  "${network}.${sitenu} is up"        else                echo "${network}.${sitenu} is down"        fidone

使用變數的值和變數賦值的區別是,是否存在首碼$

ls -lh $dir > /dev/null 如果是這樣寫的話,完了,沒有輸出! -d $dir 判斷目錄是否存在,注意寫法
#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p "please input a directory:" dirif [ $dir = '' -o ! -d $dir ]; then        echo "the $dir is not existed in the system"        exit 1fifor info in $( ls $dir)do        perm=''        test -r "$dir/$info" && perm="$perm readable"        test -w "$dir/$info" && perm="$perm writeable"        test -x "$dir/$info" && perm="$perm executable"        echo "the $dir/$info permission $perm"done
for迴圈的另一種輸入方式for (( 初始值; 限定值; 步長 ))do    程式段done
#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p "please input a number:" nudeclare -i sum=0for((i=1; i<=$nu; i=i+1))#注意,這裡並沒有使用$ido        sum=$(($sum+$i))doneecho "the total is : $sum"這裡測試加上$#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p "please input a number:" nudeclare -i sum=0for((i=1; $i<=$nu; i=$i+1))        #修改成了一般的認識,發現仍然可以,感覺這種更符合平常邏輯do        sum=$(($sum+$i))doneecho "the total is : $sum"下面的錯誤應該不是$i的寫法的,不明白哪裡語法錯誤了fuhui@ubuntu:~/script$ sh -n sh27.sh sh27.sh: 9: sh27.sh: Syntax error: Bad for loop variable

判斷指令碼是否存在語法錯誤

[root@www ~]# sh [-nvx] scripts.sh  

選項參數:

-n :不執行script,僅查詢文法是否存在問題;-v :再執行 sccript 前,先將 scripts 的內容輸出到螢幕上;-x :將使用到的 script 內容顯示到螢幕上,這是很有用的參數!
fuhui@ubuntu:~/script$ sh -x sh27.sh + PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin+ export PATH+ read -p please input a number: nuplease input a number:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.