使用shell指令碼修改檔案引數 範例 If語句

2021-09-24 17:19:33 字數 961 閱讀 5900

下例是本人親測臨時寫的乙個實用指令碼disable selinux模組的

#!/bin/bash

selinux=\$(grep ^selinux=.* /etc/selinux/config | cut -d "=" -f2)

if [ "$selinux" == "diabled" ]

then echo "your selinux is diable already and no need to change"

else sed -i '1,30s/selinux=.*/selinux=disabled/g' /etc/selinux/config

fi

關於指令碼一定要留意空格

比如檔案對比 == 兩邊是有空格

括號內容之間有空格

賦予變數$()這個符號間是沒有空格的

if 條件測試命令1 ; then

命令序列1

elif 條件測試命令2 ; then

命令序列2

elif …

else

命令序列n

fi判斷服務啟用例項

#!/bin/bash

#判斷iptables是否在執行,如果已經在執行提示資訊,如果沒有開啟它。

service iptables status &> /dev/null

if [ $? -eq 0 ];

then

echo "iptables service is running"

else

service iptables restart

fi

shell指令碼檔案引數

本文編輯自 當我們我們向 指令碼檔案傳遞引數 可以通過 1,2 等特殊變數。很方便,但是有些限制,就是不能超過9個引數。通過使用 shift getopts 我們能更方便地提取引數。一 shift 通過使用 shift 我們將 shell指令碼檔案 的引數起點從左向右移。在shift命令中可以給乙個...

使用shell指令碼執行sql檔案 傳遞引數

終端執行 日誌及結果直接列印 sh x.sh 後台執行 日誌存在nohup.out 防止斷網時任務中斷 nohup sh x.sh hive hivevar datebuf f x.sql x.txt 等於hive d datebuf f x.sql x.txt 1.變數中 可帶可不帶,即 可以修改...

批量修改檔案字尾 shell指令碼

ps 當前目錄下,無遞迴查詢,其中hz1為修改前的字尾,hz2為修改後的 bin bash hz1 1 hz2 2if 1 then hz1 repo hz2 bak echo 預設原始檔字尾為 repo 修改後字尾為 bak fipwd pwd echo pwd if pwd then echo ...