shell基礎練習

2021-08-15 15:33:35 字數 2496 閱讀 6038

shell基礎練習

1、編寫shell指令碼,實現1-100的猜數字遊戲。

#!/bin/bash

##random隨機函式,100取餘就可以獲得1-100的隨機整數

n=$[$random%100]

while :

doread -p "請輸入乙個1-100間的整數:" n1

n2=`echo $n1|sed 's/[0-9]//g'`

if [ ! -z $n2 ]

then

echo "你輸入的不是1-100的整數!"

continue

fiif [ $n1 -gt 100 ] || [ $n1 == 0 ]

then

echo "請輸入1-100的整數!"

continue

fiif [ $n1 == $n ]

then

echo "你猜對了!"

break

elif [ $n1 -gt $n ]

then

echo "你輸入的數字太大了!"

continue

else

echo "你輸入的數字太小了!"

2、編寫shell指令碼,列印乘法口訣表

#!/bin/bash

for i in `seq 1 9`

do for j in `seq 1 $i`

dok=$[$i*$j]

##-n選項,不換行,-e使用製表符

3、編寫shell指令碼,計算1-100的和

4、編寫shell指令碼,要求輸入乙個正整數,然後計算出從1到輸入數字的和,要求如果輸入的數字小於1,則重新輸入,直到輸入正確的數字為止;

#!/bin/bash

while :

doread -p "請輸入乙個正整數: " n

if echo $n |grep -q '[^0-9]'

then

echo "你沒有輸入乙個正整數!"

continue

fiif [ $n -lt 1 ]

then

echo "你沒有輸入大於1的數!"

5、編寫shell指令碼,把/root/目錄下的所有目錄(只需要一級)拷貝到/tmp/目錄下;

6、編寫shell指令碼,批量建立使用者user_00,user_01,...user_100並且所有使用者同屬於users組;

shell基礎練習

shell基礎練習 1 編寫shell指令碼,實現1 100的猜數字遊戲。bin bash random隨機函式,100取餘就可以獲得1 100的隨機整數 n random 100 while doread p 請輸入乙個1 100間的整數 n1 n2 echo n1 sed s 0 9 g if ...

shell基礎練習

1.shell變數和陣列,返回值 變數 root gimi1 a test root gimi1 echo a test root gimi1 a 1 root gimi1 echo a a陣列 取單個值 root gimi1 echo 從左邊往右取 1 root gimi1 echo 從右邊往左取...

shell編寫基礎練習

shell script練習 1.設定和顯示變數,編寫乙個名為sayhello的script,放置於你的啟動檔案中,當你登陸進系統時能根據當時系統時間顯示一條歡迎資訊,比如 good morning good afternoon good evening 1 script bin bash if s...