Linux shell篇之exit用法

2021-10-01 23:41:39 字數 840 閱讀 7143

exit:退出指令碼

exit #

(#可以使用0至255,0表示正確,其他表示不同的錯誤)

如果指令碼沒有明確定義退出狀態碼,那麼,最後執行的一條命令的退出碼即為指令碼的退出狀態碼

bash中常用的條件測試有三種:

整數測試:

-gt-le

-ne-eq

-ge-lt

int1=63

int2=77

[ $int1 -eq $int2 ]

檔案測試:

-e file:測試檔案是否存在

-f file:測試檔案是否為普通檔案

-d file:測試指定路徑是否為目錄

-r file:測試當前使用者指定檔案是否有讀取許可權

-w file:測試當前使用者指定檔案是否有寫許可權

-x file:測試當前使用者指定檔案是否有執行許可權

#!/bin/bash

file=/etc/inittab

if [ ! -e $file ]; then

echo 「no $file.」

exit 8

ifif grep 「^$」 $file &> /dev/null; then

echo 「total blank lines: `grep 「^$」 $file | wc -l`.」

else

echo 「no blank line.」

fi測試指令碼是否有語法錯誤:

bash -n 指令碼

例如:bash -n filetest.sh

指令碼單步執行(很實用):

bash -x 指令碼

例如:bash -x filetest.sh

Linux shell篇之bash特性

bash特性說明 shell 外殼 gui gnome,kde,xfce cli sh,csh,ksh,bash 最全最強大 tcsh,zsh 沒怎麼推廣 1.1 命令歷史 檢視命令歷史 history c 清空命令歷史 d 刪除指定位置的命令 history d 500 3 第500行開始刪除3行...

Linux shell篇之sed用法

sed option addresscommand file n 靜默模式,不再預設顯示模式空間中的內容 sed n p etc fstab,只顯示符合條件的行 i 直接修改原檔案 e script e script 可以同時執行多個指令碼 f path to sed script sed f pa...

Linux shell篇之ps命令

ps process state sysv風格 需要加橫槓 bsd風格 不需要加橫槓 a 所有與終端有關的程序 u x 所有與終端無關的程序 例子 ps aux head 程序狀態 d 不可中斷的睡眠 r 執行或就緒 s 可中斷的睡眠 t 停止 z 僵死 高優先順序程序 n 低優先順序程序 前台程序...