當前位置:首頁 » 網頁前端 » 一起來寫一個shell腳本吧
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

一起來寫一個shell腳本吧

發布時間: 2022-02-15 00:13:07

㈠ SHELL寫一個腳本

如果刪除/xxx/xxxxxx/xxx/下 3天以前的 所有內容:1find /xxx/xxxxx/xxx/* -mtime +3 -exec rm -rf {} \;如果刪除/xxx/xxxxxx/xxx/下 3天以前的 所有文件(不包含目錄):1find /xxx/xxxxx/xxx/* -type f -mtime +3 -exec rm -rf {} \;如果不刪除MSS目錄 只刪除MSS內的所有 3天以前的 文件:1find /xxx/xxxxx/xxx/MSS[0-9]*/* -type f -mtime +3 -exec rm -rf {} \;如果不刪除MSS目錄 但MSS下所有 3天以前的 目錄及文件全刪除:1find /xxx/xxxxx/xxx/MSS[0-9]*/* -mtime +3 -exec rm -rf {} \;

㈡ 如何寫一個shell腳本

簡單的說,你把你在終端輸入的命令放在一個文件里,這個文件就成了shell腳本
比如你編輯一個文件叫 helloworld.sh
內容是
echo "Hello world!"

你把helloword.sh變成可執行的,就是在終端輸入
chmod +x helloworld.sh
然後運行
./helloworld.sh

你就成功滴寫好了一個shell腳本

㈢ 寫一個Shell腳本

此問題其實可以用find命令解決
find dir -t f -empty |xargs rm -f,這樣便達到要求了。若想自已用shell實現,我這里有一個,僅做參考。(主要缺點在於,用了遞歸,沒有測試最大能夠處理到多少級子目錄)
#!/bin/bash -
#by volate
set -u

Pro=$0
Dir=$1

#此函數標明此程序的用法,或者因錯誤退出的標志及退出碼
usage(){
local pro=$1
local ErrStr=$2
local ExitDig=$3
echo "Usage: $pro {Dir}"
echo "Error: $ErrStr"
exit $ExitDig
}

#此函數找出目標目錄下的所有文件,並遞歸到子目錄
findFile(){
local dir=$1
cd $dir || usage $Pro "Can't chdir $dir" 2
for f in * ;do
#遞歸到子目錄
test -d $f && findFile $f
#若是文件,則交給下個函數處理
test -f $f && delEmptyFile $f
done
}

#此函數刪除大小為0的文件
delEmptyFile(){
local file=$1
#得到文件大小
local fileSize=`ls -l $file | awk '{print $5}'`
#判斷大小是否為0
if [ "X$fileSize" = "X0" ]; then
echo "`pwd`/$file is empty, delete..."
rm -f $file || usage $Pro "can't delete $file" 3
fi
}

Pro=$0
if [ $# != 1 ];then
usage $Pro "argv error" 1
fi

Dir=$1
workDir=`pwd`
findFile $Dir
cd $workDir
exit 0

㈣ 請編寫一個shell腳本

條件寫得倒是很詳細的,但發現用腳本不一定要死要求,有時靈活一點更有效率。
如果覺得還不賴,就拿去用吧。
#!/bin/bash

echo "Please Enter a IP of C class"
read ip
i=1
while [[ True ]]
do
if [[ $i -gt 255 ]]; then
echo $i
exit 0
fi
echo "$ip.$i"
ping -c1 -w1 $ip.$i &>/dev/null && echo "$ip.$i">>goodhost || echo "$ip.$i">>badhost
i=`expr $i + 1`
done

㈤ 幫忙寫一個linux shell腳本

簡單寫下,大概就是這樣子了。要用的話還要自己修改了。。。。。。。

while true

do
md5sum /path/testda.file > testda.file.md5
for ka in disk{b..m} ; do sed "s@\/path\/testda.file@\/mnt$ka\/testda.file@g" testda.file.md5 >> testda.file.2.md5 ; done

dd if=/path/testda.file of=/mnt/disk{b..m}/testda.file
md5sum -c testda.file.2.md5

rm -f /mnt/disk{b..m}/testda.file
rm -f testda.file{.2}.md5

done

㈥ 編寫一個簡單Shell腳本完成

#!/bin/sh
who >file
more file

----------------------
其實可以同時實現查看who命令結果和重定向到文件,用雙向重導向命令tee就行了:
who|tee file

㈦ 編寫一個shell腳本

一樓的可以實現咯,如果不想用現成的rev的話,可以自己寫一個:

#!/bin/bash

str=$*
let i=${#str}-1
while [[ i -ge 0 ]]; do
echo -n ${str:$i:1}
let i=$i-1
done
echo

㈧ 編寫一個shell腳本程序

awk'{print$0,$3+$4+$5}'bjcj.txt|sort-nr-k6|head-5

awk計算總成績,並放置原數據後面一列並顯示,sort是按照總成績(第六列)從大到小的數值進行排序,head篩選出前五行數據