文章目錄
- 實驗一:利用資料生成程式熟練掌握Shell腳本對檔案夾的批處理
- 實驗二:在Linux環境下撰寫一個壓縮和解壓縮的小工具
- 實驗三:在Linux下利用case陳述句撰寫腳本,實作建立用戶和洗掉用戶的功能,過ssh連接到IP主機并保持登錄的功能,
- 實驗四:在Linux下實作一個檔案及檔案夾操作的Shell腳本
實驗一:利用資料生成程式熟練掌握Shell腳本對檔案夾的批處理
1.benchmark為一個可執行檔案,用于生成我們需要的資料集,運行該檔案的命令為./benchmark,
該程式可執行檔案的輸入檔案為該檔案所在目錄下的parameters.dat,輸出檔案為statistics.dat,comunity.dat,network.dat,且該程式每次運行生成的資料集不同,
2.parameters.dat中包含的引數為number, averagePower, maxPower, moreno, morenO, mix,max, min,存放格式如parameters.dat所示,
3.現我們需要寫一個腳本,完成如下作業,
(1)生成parameters.dat檔案,用寫入的方式將引數寫入該檔案,
(2)在相同引數下運行benchmark檔案10次,并根據引數和次數新建檔案夾,檔案夾名字如:1000_10_30_2_1_0.3_3_16_10,最后的數字代表第多少次生成,
(3)每次產生輸出檔案時將三個輸出檔案存盤在不同的檔案夾,并洗掉該次的引數存放檔案:parameters.dat檔案,
#!/bin/bash
#test1.sh
#將parameter.dat.txt資料寫入檔案parameter.dat,并運行benchmark10次,新建檔案夾,并輸出,然后洗掉parameters.dat檔案,
#創建檔案parameter.dat
touch parameters.dat
#將parameter.dat.txt的內容復制到parameter.dat中
cat parameters.dat.txt>>parameters.dat
for((i=1;i<=10;i++))
do
`./benchmark`
`mkdir 1000_10_30_2_1_0_0.3_3_16_${i}`
`mv ./statistics.dat 1000_10_30_2_1_0_0.3_3_16_${i}/statistics.dat`
`mv ./community.dat 1000_10_30_2_1_0_0.3_3_16_${i}/community.dat`
`mv ./network.dat 1000_10_30_2_1_0_0.3_3_16_${i}/network.dat`
done
rm -rf parameters.dat
運行截圖



實驗二:在Linux環境下撰寫一個壓縮和解壓縮的小工具
要求:壓縮的時候可以由用戶輸入密碼,解壓縮的時候需要提供密碼才能解壓縮,
#!/bin/bash
#test2.sh
#encode to zip or unzip file
echo -e '1.zip flle\n2.unzip file\n'
read choice
case $choice in
1)
echo "please input the filename:"
read filename
echo "please input the zip code"
read password
`zip -rP ${password} ${filename}.zip ${filename}`
echo "zip successfully";;
2)
echo "please input the filename"
read filename
echo "please input the zip code"
read password
`unzip -P ${password} ${filename}`
echo "unzip successfully";;
esac
運行截圖:




實驗三:在Linux下利用case陳述句撰寫腳本,實作建立用戶和洗掉用戶的功能,過ssh連接到IP主機并保持登錄的功能,
實驗第一部分實作的功能為:
1.執行create時根據userfile和passfile建立用戶,
2.執行delete時根據userfile洗掉用戶,
實驗第二部分實作的功能為:
1.執行:/mnt/auto_connect.exp IP password 時保證密碼正確,那么就通過 ssh 連接到該 IP 主機,并保持登陸,
第一部分代碼:
#!/bin/bash
#test3.sh
#create user and delete user
echo -e "please input userfilename:"
read userfile
echo -e "please input the passfile"
read passfile
echo -e "create:create new users by $userfile and $passfile\ndelete:delete all users by $userfile"
read choice
export username
case $choice in
'create')
if [ ! -e $userfile ] || [ ! -e $passfile ]
then
echo "$userfile of $passfile do not exist"
exit 1
fi
#user number
line1=`cat $userfile|wc -l`
#passwd number
line2=`cat $passfile|wc -l`
#judge the user number and passwd number
echo "line1:$line1"
echo "line2:$line2"
if [ $line1 -ne $line2 ]
then
echo "user number is not equal with passwd number"
exit 1
fi
#read userfile
for((i=1;i<=3;i++))
do
read username[i]
`sudo useradd ${username[i]}`
done<${userfile}
#read passwdfile
for((i=1;i<=3;i++))
do
read password[i]
`echo ${username[i]}:${password[i]} | sudo chpasswd`
echo "${username[i]}用戶創建成功"
done<${passfile};;
'delete')
if [ -e $suerfile ]
then
echo "deleting"
else
echo "file do not exist"
exit 1
fi
for((i=1;i<=3;i++))
do
read username
echo "${username}用戶洗掉成功"
`sudo userdel ${username}`
done<$userfile;;
esac
echo "successfully"
第二部分代碼:
需要主機配置ssh環境
#!/bin/bash
#test3_plus.sh
#于主機建立ssh連接
/usr/bin/expect << EOF
spawn ssh root@$1
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "$2\r" }
}
interact
EOF
運行截圖:
第一部分:




第二部分截圖:

實驗四:在Linux下實作一個檔案及檔案夾操作的Shell腳本
1.新建一個test檔案夾,在下面新建10檔案夾,每一個檔案夾下新建一個txt檔案,并寫入內容,
2.獲取目錄下所有檔案的檔案名,(包括路徑)
3.將test檔案夾下10個檔案夾下的檔案復制到test檔案夾下的data檔案夾中,將data檔案夾中所有檔案重定向到一個新的檔案中,
cd `dirname $0`
mkdir test
cd ${PWD}/test
#列印當前路徑
echo ${PWD}
for((i=0;i<10;i++))
do
#新建檔案夾
eval "mkdir" "test${i}"
#新建檔案
touch test${i}/new${i}.txt
#寫入內容
ls -l>test${i}/new${i}.txt
done
touch filenames.txt
for((i=0;i<10;i++))
do
#進入目標檔案夾
cd test${i}
#將當前路徑寫入filenames.txt
echo ”$PWD”>>../filenames.txt
#回傳上級目錄
cd ..
done
mkdir data
for((i=0;i<10;i++))
do
#移動檔案夾
mv test${i}/new${i}.txt data
`cat data/new${i}.txt>>newAll.txt`
done
#回傳上級目錄
cd ..







轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/395327.html
標籤:其他
