linux shell命令

echo:顯示文本行或變量取值,或者把字符串輸入到文件中

echo中常用功能:\t 跳格  \n 換行  必須使用 -e才能轉義符生效

echo $變量名 變量前必須加上$age=18   echo $age
echo -e 輸出變量,並跳格 \techo -e  “hello\tworld”
輸出變量,並換行 \n echo -e  “hello\nworld”

read:從鍵盤或者文件的某一行文本中讀入信息,並將其賦給一個變量;

輸入內容以空格分隔,與變量一一對應。

如果輸入的值個數多於變量個數,多餘的值會賦給最後一個變量。

如果輸入的值個數少於變量個數,按空格一一對應賦值,多餘的變量爲空


read var 賦值 read name

tom

echo $name

read var1 var2 "輸入的值多餘變量個數, read name age

多餘的值賦值給最後一個變量" hyh 12 ttt    //賦值

echo $age  
12 ttt          //輸出j結果  

tee:把一個命令的輸出內容拷貝到另一個文件,一般和管道符|一起使用  

-a 表示將內容追加到文件末尾

file 表示保存輸出結果的文件(文件所在路徑必須存在,文件存在與否不作硬性要求)
teetee -a file 把一個命令的輸出內容拷貝到另一個文件 echo "helloworld" | tee -a home1
command > file 輸出內容寫入到一個文件,錯誤仍然輸出屏幕 cat hello.txt > hello1.txt
command >>file 輸出內容追加寫入到一個文件 cat hello.txt >> hello1.txt
command 1> file 輸出內容向到一個文件 cat hello.txt 1> hello1.txt
command 2>> file 錯誤內容追加寫入到一個文件 cat hello 2>> hello1.txt
command >file 2>&1 輸出內容和錯誤內容一起寫入到一個文件 cat hello hello.txt > hello1.txt 2>&1
command >>file 2>&1 輸出內容和錯誤內容一起追加寫入到一個文件 cat hello hello.txt >> hello1.txt 2>&1
command 1> file1 2> file2 輸出內容寫入一個文件,錯誤內容寫入另一個文件 cat hello hello.txt 1> hello1.txt 2> hello2.txt
command < file以file 文件內容做爲文件輸入 read name age < user.txt
comamnd < file1 > file2以file1 文件內容做爲文件輸入,file2做爲輸出 cat <user.txt > user1.txt
Shell後臺命令

crontab執行步驟:

1.進入管理員權限,執行命令 yum -y install vixie-cron 安裝crontab

2.輸入命令 service crond status 查看crontab服務是否啓動;

    可通過命令 service crond start\stop\restart 對服務進行啓動、關閉和重啓

注:通過Yum安裝的服務在service命令中,服務名後都要加d,如crond

3.在crontab後臺執行命令

4.創建Shell腳本,一般以file.sh命名並編寫操作命令。(腳本中的路徑皆爲絕對路徑)

5.通過命令直接執行腳本或將執行命令放入到後臺執行程序中執行

    方式一: 路徑+腳本名(要求腳本必須有執行權限) => ./helloworld.sh

   方式二:sh + 腳本名  =>  sh helloworld.sh 

crontab 格式: 分 時 日 月 星期 運行命令

1、表示範圍:週一到週五 => 1-5

2、表示某些值: 10點和 20點 => 10,20

3、表示每隔某些值: 每隔5分鐘 => */5

7月10月4-10日這期間的23點3點,每隔15分鐘執行一次輸出helloworld
crontab -e 打開crontab腳本 執行shell腳本 */15 3,23 4-10 7,10 * /home/hyh/class19/shell/backup.sh
at 後臺執行命令,只執行一次
"$at 19:10 2017-7-5 echo "hello" ctrl+ d//結束編輯
atq / at -l 查看任務
at -c 任務編號查看某個具體任務
atrm 刪除某個任務
at 19:10 2017-7-5 -f 執行腳本 "at 10:15 2017-7-5 -f  /home/hyh/class19/shell/edit.sh"
系統-用戶變量
export 變量名 age=18 export age 將變量age變爲系統變量
unset 變量名 釋放變量 unset age
env 查看系統變量env | grep age
set 查看用戶變量set | grep age
readonly 變量名 readonly age 將變量age設爲只讀模式,用戶退出shell後設置取消
位置-特定變量 

位置變量:只讀變量,向shell腳本傳遞參數,參數個數可以任意多,但只有前9個有效表示形式爲: $1,$2.....$9

特定變量:只讀變量,反映腳本運行過程中的控制信息

$! :顯示後臺最後運行的一個進程的進程號

$# :顯示傳遞到shell腳本中參數的總個數

$@:顯示所有的參數,使用時加引號,並返回每個參數

$* :顯示所有的參數

$$ :腳本運行的當前進程號

$? :顯示最後執行的命令的狀態,0爲正確,非0爲錯誤


#!/bin/bash shell/local.sh 2 2 3 4 5 6 7 8 9 10 11
echo "give 1 you have $1"give 1 you have 2
echo "give 2 you have $2"give 2 you have 2
echo "give 3 you have $3"give 3 you have 3
echo "give 4 you have $4"give 4 you have 4
echo "give 5 you have $5"give 5 you have 5
echo "give 6 you have $6"give 6 you have 6
echo "give 7 you have $7"give 7 you have 7
echo "give 8 you have $8"give 8 you have 8
echo "give 9 you have $9"give 9 you have 9
echo "give 10 you have $10"give 10 you have 20
echo "give 11 you have $11"give 11 you have 21
echo "give \$# you have $#"give $# you have 11
echo "give \$@ you have $@"give $@ you have 2 2 3 4 5 6 7 8 9 10 11
echo "give \$* you have $*"give $* you have 2 2 3 4 5 6 7 8 9 10 11
echo "give \$! you have $!"give $! you have
echo "give \$$ you have $$"give $$ you have 4709
echo "give \$? you have $?"give $? you have 0
發佈了27 篇原創文章 · 獲贊 8 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章