歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux常用命令(九)上機操作操作題解答

Linux常用命令(九)上機操作操作題解答

日期:2017/3/3 12:40:00   编辑:Linux技術

1. 某系統管理員需每天做一定的重復工作,請按照下列要求,編制一個解決方案:

(1)在下午4 :50刪除/abc目錄下的全部子目錄和全部文件;

(2)從早8:00~下午6:00每小時讀取/xyz目錄下x1文件中每行第一個域的全部數據加入到/backup目錄下的bak01.txt文件內。

解答:

crontab -e 打開命令然後輸入

(1) 50 16 * * * rm -rf /abc

(2)0 8-18/1 * * * cut -f1 /xyz/x1>> /backup/bak01.txt

2.編寫一個名為mul的腳本程序,參數為一個大於20的正整數。先檢查參數是否符合要求。如果不符合要求,請給出提示;如果符合要求,分別輸出其與1到10的乘積。

#gedit mul.sh

#!/bin/bash

if [ $1 -gt 20 ]

then

n=1

m=1

while [ $n -le 10 ]

do

m=$(expr $1 \* $n)

echo "$n $m"

n=$(expr $n + 1)

done

else

echo "number iswrong"

fi

#bash mul.sh 34

3.編寫一個名為move的腳本程序,格式move <file1> <file2>。如果file1不存在,給出提示;否則移動file1至file2。

#gedit move.sh

#! /bin/bash

if test -f file1

then mv file1 file2

else

echo "file1 isnot exists"

fi

# bash move.sh file1 file2

4.編寫一個shell腳本,能夠顯示下面序列的前25個數字。0,1,1,2,3,5,8,13…,前二個數字之和為第三個數字,即著名的Fibonacci序列。

#gedit shell.sh

#!/bin/bash

n=0

echo "$n "

m=1

echo "$m "

t=1

a=2

while [ $a -lt 25 ]

do

t=$(expr $n + $m)

echo "$t "

a=$(expr $a + 1)

n=$m

m=$t

done

# bash shell.sh

5.編寫一個名為square的腳本程序,參數為一大於10的正整數。先檢查參數是否符合要求。如果不符合要求,請給出提示;如果符合要求,輸出從1到該正整數的平方值。

#gedit square.sh

#!/bin/bash

if [ $1 -gt 10 ]

then

n=1

m=1

while [ $n -le $1 ]

do

m=$(expr $n \* $n)

echo "$n $m "

n=$(expr $n + 1)

done

else

echo "number iswrong"

fi

#bash square.sh 45

Copyright © Linux教程網 All Rights Reserved