習題要求 創建好作業後,先進入文件夾/home/acs/homework/lesson_3/,然後: (0) 進入homework_0文件夾,編寫自動完成lesson_1作業的腳本helper.sh。要求: [1] 當前目錄下僅包含helper.sh [2] helper.sh具有可執行許可權 [3] ...
習題要求
創建好作業後,先進入文件夾/home/acs/homework/lesson_3/,然後:
(0) 進入homework_0文件夾,編寫自動完成lesson_1作業的腳本helper.sh。要求:
[1] 當前目錄下僅包含helper.sh
[2] helper.sh具有可執行許可權
[3] 在任意路徑依次執行下列命令後,lesson_1的作業可以得到滿分:
1) homework 1 create
2) /home/acs/homework/lesson_3/homework_0/helper.sh
(1) 進入homework_1文件夾,編寫腳本check_file.sh。要求:
[1] 當前目錄下僅包含check_file.sh。
[2] check_file.sh具有可執行許可權。
[3] check_file.sh接收一個傳入參數。格式為 ./check_file.sh file
[4] 判斷傳遞參數,分別在標準輸出中輸出如下內容(不包括雙引號):
1) 如果傳入參數個數不是1,則輸出一行:"arguments not valid",然後退出,退出狀態等於1。
2) 如果file文件不存在,則輸出一行:"not exist",然後退出,退出狀態等於2。
3) 如果file文件存在,則輸出分別進行如下5個判斷,然後退出,退出狀態等於0。
1] 如果file為普通文件,則輸出一行:"regular file"
2] 如果file為目錄(文件夾),則輸出一行:"directory"
3] 如果file具有可讀許可權,則輸出一行:"readable"
4] 如果file具有可寫許可權,則輸出一行:"writable"
5] 如果file具有可執行許可權,則輸出一行:"executable"
(2) 進入homework_2文件夾,編寫腳本main.sh。要求:
[1] 當前目錄下僅包含main.sh
[2] main.sh具有可執行許可權
[3] 該文件從stdin(標準輸入)中讀取一個整數n
[4] 在stdout(標準輸出)輸出斐波那契數列的第n項。即:a[0] = 1, a[1] = 1, a[i] = a[i - 1] + a[i - 2], 求a[n]。
[5] 數據保證 0 <= n <= 20,腳本不需要判斷n的合法性。
(3) 進入homework_3文件夾,編寫腳本main.sh。要求:
[1] 當前目錄下僅包含main.sh
[2] main.sh具有可執行許可權
[3] 該文件從stdin(標準輸入)中讀取兩行整數n和m
[4] 在stdout(標準輸出)中輸出1~n的按字典序從小到大的順序數第m個全排列,輸出一行,用空格隔開所有數,行末可以有多餘空格。
[5] 數據保證 1 <= n <= 10, 1 <= m <= min(100, n!),腳本不需要判斷數據的合法性。
(4) 進入homework_4文件夾,編寫腳本main.sh。要求:
[1] 當前目錄下僅包含main.sh
[2] main.sh具有可執行許可權
[3] main.sh接收兩個傳入參數。格式為 ./main.sh input_file output_file
[4] 從input_file中讀取一個正整數n,然後將前n個正整數的平方和寫入output_file中
[5] 數據保證 1 <= n <= 100,腳本不需要判斷所有數據的合法性。
第零題
創建好作業後,先進入文件夾/home/acs/homework/lesson_3/,然後:
(0) 進入homework_0文件夾,編寫自動完成lesson_1作業的腳本helper.sh。要求:
[1] 當前目錄下僅包含helper.sh
[2] helper.sh具有可執行許可權
[3] 在任意路徑依次執行下列命令後,lesson_1的作業可以得到滿分:
1) homework 1 create
2) /home/acs/homework/lesson_3/homework_0/helper.sh
homework 3 create
cd /home/acs/homework/lesson_3/homework_0
ls -a
vim helper.sh :wq
ls -l helper.sh
chmod +x helper.sh
vim helper.sh
# helper.sh內容如下:
#! /bin/bash
# 不能加空格
homework1_dir=/home/acs/homework/lesson_1/homework_
homework 1 create 0
# ${}是變數替換,$()是返回stdout
cd ${homework1_dir}0
for i in dir_a dir_b dir_c
do
mkdir $i
done
homework 1 create 1
cd ${homework1_dir}1
for i in a.txt b.txt c.txt
do
cp $i ${i}.bak
done
homework 1 create 2
cd ${homework1_dir}2
for i in a b c
do
mv ${i}.txt ${i}_new.txt
done
homework 1 create 3
cd ${homework1_dir}3
for i in a.txt b.txt c.txt
do
mv dir_a/${i} dir_b
done
homework 1 create 4
cd ${homework1_dir}4
for i in a.txt b.txt c.txt
do
rm ${i}
done
homework 1 create 5
cd ${homework1_dir}5
for i in dir_a dir_b dir_c
do
# echo $(pwd)/${i}
rm ${i} -r
done
homework 1 create 6
cd ${homework1_dir}6
# done是關鍵字要使用雙引號,否則會找do去匹配
# mv task.txt dir_a/done.txt
mkdir dir_a
mv task.txt "dir_a/done.txt"
homework 1 create 7
cd ${homework1_dir}7
for ((i=0;i<3;i++))
do
mkdir dir_${i}
# for ((j=0;j<3;j++))
# do
# # 會操作3x3x3條
# cp a.txt dir_${i}/a${j}.txt
# cp b.txt dir_${i}/b${j}.txt
# cp c.txt dir_${i}/c${j}.txt
# done
for j in a b c
do
cp ${j}.txt dir_${i}/${j}${i}.txt
done
done
homework 1 create 8
cd ${homework1_dir}8
rm dir_a/a.txt
mv dir_b/b.txt dir_b/b_new.txt
cp dir_c/c.txt dir_c/c.txt.bak
homework 1 create 9
cd ${homework1_dir}9
rm *.txt
# 每次執行完之後調用一下
# homework 1 test
# helper.sh內容結束
# 註意事項
1. 開頭的 `#! /bin/bash` 如果不加的話會導致for迴圈報錯
2. 要保證目錄里只有一個文件:關掉vim
3. 粘貼時要開啟粘貼模式隱藏行號 :set paste :set nonu
# 如何將伺服器中的文件整體複製出來
1. 退出tmux
2. `cat filename`:展示filename的文件內容
3. 滑鼠選中文本開頭的若幹字元
4. 用滾輪滑到文件結尾
5. 按住Shift,同時滑鼠點擊文件結尾,此時會選中文件所有內容
6. Windows/Linux下,按Ctrl + insert可以複製全文;Mac下,按Command + c可以複製全文。
第一題
(1) 進入homework_1文件夾,編寫腳本check_file.sh。要求:
[1] 當前目錄下僅包含check_file.sh。
[2] check_file.sh具有可執行許可權。
[3] check_file.sh接收一個傳入參數。格式為 ./check_file.sh file
[4] 判斷傳遞參數,分別在標準輸出中輸出如下內容(不包括雙引號):
1) 如果傳入參數個數不是1,則輸出一行:"arguments not valid",然後退出,退出狀態等於1。
2) 如果file文件不存在,則輸出一行:"not exist",然後退出,退出狀態等於2。
3) 如果file文件存在,則輸出分別進行如下5個判斷,然後退出,退出狀態等於0。
1] 如果file為普通文件,則輸出一行:"regular file"
2] 如果file為目錄(文件夾),則輸出一行:"directory"
3] 如果file具有可讀許可權,則輸出一行:"readable"
4] 如果file具有可寫許可權,則輸出一行:"writable"
5] 如果file具有可執行許可權,則輸出一行:"executable"
cd /home/acs/homework/lesson_3/homework_1
ls -a
vim check_file.sh :wq
ls -l check_file.sh
chmod +x check_file.sh
vim check_file.sh
# ./check_file.sh file
#! /bin/bash
if [ $# -ne 1]
then
echo arguments not valid
exit 1
fi
# 中間帶空格的字元串要加雙引號
if [ ! -e "$i" ]
then
echo not exist
exit 2
fi
if [ -f "$1" ]
then
echo regular file
#exit 0 # 預設就是返回0,不寫也行
fi
if [ -d "$1" ]
then
echo directory
fi
if [ -r "$1" ]
then
echo readable
fi
if [ -w "$1" ]
then
echo writable
fi
if [ -x "$1" ]
then
echo executable
fi
第二題
(2) 進入homework_2文件夾,編寫腳本main.sh。要求:
[1] 當前目錄下僅包含main.sh
[2] main.sh具有可執行許可權
[3] 該文件從stdin(標準輸入)中讀取一個整數n
[4] 在stdout(標準輸出)輸出斐波那契數列的第n項。即:a[0] = 1, a[1] = 1, a[i] = a[i - 1] + a[i - 2], 求a[n]。
[5] 數據保證 0 <= n <= 20,腳本不需要判斷n的合法性。
cd /home/acs/homework/lesson_3/homework_2
ls -a
vim main.sh :wq
ls -l main.sh
chmod +x main.sh
vim main.sh
#! /bin/bash
read n
# 等號兩邊不能加空格
a[0]=1
a[1]=1
for ((i=2;i<=n;i++))
do
x=$(expr &i - 1) # x = a[i-1]
y=$(expr &i - 2)
a[$i]=$(expr ${a[$x]} + ${a[$y]}) # a[i] = a[i-1] + a[i-2]
done
echo ${a[$n]}
第三題
(3) 進入homework_3文件夾,編寫腳本main.sh。要求:
[1] 當前目錄下僅包含main.sh
[2] main.sh具有可執行許可權
[3] 該文件從stdin(標準輸入)中讀取兩行整數n和m
[4] 在stdout(標準輸出)中輸出1~n的按字典序從小到大的順序數第m個全排列,輸出一行,用空格隔開所有數,行末可以有多餘空格。
[5] 數據保證 1 <= n <= 10, 1 <= m <= min(100, n!),腳本不需要判斷數據的合法性。
cd /home/acs/homework/lesson_3/homework_3
ls -a
vim main.sh :wq
ls -l main.sh
chmod +x main.sh
# 先用c++進行編寫
#include<iostream>
using namespace std;
const int N = 110;
int n,m; // n個數找第m個全排列
int path[N]; // 路徑數組
bool st[N]; // 狀態數組,看第i個數是否被用過
bool dfs(int u){
if(u==n){
m--;
if(!m){
for(int i=0;i<n;i++)
cout << path[i] << '';
cout << endl;
return true;
}
return false;
}
for(int i=1;i<=n;i++){
if(!st[i]){
path[u] = i;
st[i] = true;
if(dfs(u+1))
return true;
// 恢復現場
st[i] = false;
}
}
return false;
}
int main(){
cin >> n >> m;
dfs(0); //從第0位開始
return 0;
}
vim main.sh
#! /bin/bash
read n
read m
# 初始化數組
for((i=1;i<=n;i++))
do
st[$i]=0 # 0表示false
done
# dfs流程
dfs(){
if [ $1 -eq $n ]
then
m=`expr $m - 1`
if [ $m -eq 0 ]
then
echo ${path[@]}
return 0 # 正常結束
fi
return 1
fi
local j=0 # 其他層遞歸的dfs會改變原來迴圈中的j
for((j=1;j<=n;j++))
do
if [ ${st[$j]} -eq 0 ]
then
path[$1]=$j
st[$j]=1
if dfs `expr $1 + 1`
then
return 0
fi
st[$j]=0
fi
done
return 1
}
# 執行dfs,從第0位開始
dfs 0
第四題
(4) 進入homework_4文件夾,編寫腳本main.sh。要求:
[1] 當前目錄下僅包含main.sh
[2] main.sh具有可執行許可權
[3] main.sh接收兩個傳入參數。格式為 ./main.sh input_file output_file
[4] 從input_file中讀取一個正整數n,然後將前n個正整數的平方和寫入output_file中
[5] 數據保證 1 <= n <= 100,腳本不需要判斷所有數據的合法性。
cd /home/acs/homework/lesson_3/homework_4
ls -a
vim main.sh :wq
ls -l main.sh
chmod +x main.sh
input_file=$1
output_file=$2
read n < $input_file
sum = 0
for ((i=1;i<=n;i++))
do
sqr=`expr $i \* $i`
sum=`expr $sum + $sqr`
done
echo $sum > $output_file
echo 10 > input.txt
./main.sh input.txt output.txt