shell程式設計題(二十三)

2022-03-19 08:34:58 字數 3816 閱讀 2692

題目:

設計乙個shell程式,在每月第一天備份並壓縮/etc目錄的所有內容,存放在當前目錄下/root/bak(root目錄需要存在)目錄裡,且檔名,為如下形式yymmdd_etc,yy為年,mm為月,dd為日。shell程式fileback存放在/usr/bin目錄下。

答案:

#!/bin/bash

dirname=`ls ./root | grep

bak`

if [ -z "

$dirname

" ]; then

echo

$dirname

mkdir ./root/bak

cd ./root/bak

fibacketc=$(date +%y%m$d)_etc.tar

.gztar zcvf $backetc /etc

echo

$backetc

echo

"fileback finished

"

編寫任務定時器:

echo

"0 0 1 * * /bin/sh /usr/bin/fileback

" >; /root/etcbakcron

crontab /root/etcbakcron

或使用crontab -e 命令新增定時任務:

0

1 * * * /bin/sh /usr/bin/fileback

shell指令碼中 if判斷時 -a -- -z

[ -a file ]  如果 file 存在則為真。  

[ -b file ]  如果 file 存在且是乙個塊特殊檔案則為真。  

[ -c file ]  如果 file 存在且是乙個字特殊檔案則為真。  

[ -d file ]  如果 file 存在且是乙個目錄則為真。  

[ -e file ]  如果 file 存在則為真。  

[ -f file ]  如果 file 存在且是乙個普通檔案則為真。  

[ -g file ] 如果 file 存在且已經設定了sgid則為真。 [ -h file ]  如果 file 存在且是乙個符號連線則為真。  

[ -k file ]  如果 file 存在且已經設定了粘制位則為真。  

[ -p file ]  如果 file 存在且是乙個名字管道(f如果o)則為真。  

[ -r file ]  如果 file 存在且是可讀的則為真。  

[ -s file ]  如果 file 存在且大小不為0則為真。  

[ -t fd ]  如果檔案描述符 fd 開啟且指向乙個終端則為真。  

[ -u file ]  如果 file 存在且設定了suid (set user id)則為真。  

[ -w file ]  如果 file 如果 file 存在且是可寫的則為真。  

[ -x file ]  如果 file 存在且是可執行的則為真。  

[ -o file ]  如果 file 存在且屬有效使用者id則為真。  

[ -g file ]  如果 file 存在且屬有效使用者組則為真。  

[ -l file ]  如果 file 存在且是乙個符號連線則為真。  

[ -n file ]  如果 file 存在 and has been mod如果ied since it was last read則為真。  

[ -s file ]  如果 file 存在且是乙個套接字則為真。  

[ file1 -nt file2 ]  如果 file1 has been changed more recently than file2, or 如果 file1 exists and file2 does not則為真。  

[ file1 -ot file2 ]  如果 file1 比 file2 要老, 或者 file2 存在且 file1 不存在則為真。  

[ file1 -ef file2 ]  如果 file1 和 file2 指向相同的裝置和節點號則為真。  

[ -o optionname ]  如果 shell選項 「optionname」 開啟則為真。  

[ -z string ]  「string」 的長度為零則為真。  

[ -n string ] or [ string ]  「string」 的長度為非零 non-zero則為真。  

[ string1 == string2 ]  如果2個字串相同。 「=」 may be used instead of 「==」 for strict posix compliance則為真。  

[ string1 != string2 ]  如果字串不相等則為真。 

[ string1 < string2 ]  如果 「string1」 sorts before 「string2」 lexicographically in the current locale則為真。  

[ string1 > string2 ]  如果 「string1」 sorts after 「string2」 lexicographically in the current locale則為真。  

[ arg1 op arg2 ] 「op」 is one of -eq, -ne, -lt, -le, -gt or -ge. these arithmetic binary operators return true if 「arg1」 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to 「arg2」, respectively. 「arg1」 and 「arg2」 are integers.

基本上和其他指令碼語言一樣。沒有太大區別。不過值得注意的是。裡面的條件判斷。

1、字串判斷

str1 = str2      當兩個串有相同內容、長度時為真 

str1 != str2      當串str1和str2不等時為真 

-n str1        當串的長度大於0時為真(串非空) 

-z str1        當串的長度為0時為真(空串) 

str1           當串str1為非空時為真

2、數字的判斷

int1 -eq int2    兩數相等為真 

int1 -ne int2    兩數不等為真 

int1 -gt int2    int1大於int2為真 

int1 -ge int2    int1大於等於int2為真 

int1 -lt int2    int1小於int2為真 

int1 -le int2    int1小於等於int2為真

3、檔案的判斷

-r file     使用者可讀為真 

-w file     使用者可寫為真 

-x file     使用者可執行為真 

-f file     檔案為正規檔案為真 

-d file     檔案為目錄為真 

-c file     檔案為字元特殊檔案為真 

-b file     檔案為塊特殊檔案為真 

-s file     檔案大小非0時為真 

-t file     當檔案描述符(預設為1)指定的裝置為終端時為真

4、複雜邏輯判斷

-a         與 

-o        或 

!        非

**:

隨筆 二十三

1.gradle 完a整指南 android 2.android gradle 外掛程式 3.0 挖坑日記 3.android效能優化全方面解析 4.android studio 3.2新功能特性 5.重新認識 androidstudio 和 gradle,這些都是你應該知道的 6。美團外賣andr...

演算法(二十三)

1 給定乙個整數陣列nums,找到乙個具有最大和的連續子陣列 子陣列最少包含乙個元素 返回其最大和。public int maxsubarray int nums return maxsum 複雜度分析 public int maxsubarray int nums return maxsum 2 ...

二十三 管道

在go語言中可以通過chan來定義管道,可以通過操作符 和 對管道進行讀取和寫入操作 通過管道維護例程狀態 使用make函式初始化,make chan type make chan type,len 不帶len引數的用於建立無快取區的管道,使用len建立指定緩衝區長度的管道 讀寫管道 可通過操作符 ...