shell 指令碼程式設計

2021-09-25 10:27:43 字數 2669 閱讀 6923

1、簡單指令碼程式設計1

[root@localhost desktop]# cat asr.sh 

#!/bin/bash

#this is a shell script

echo $0

echo $#,$*

echo $1,$3,$5

[root@localhost desktop]#

2、簡單指令碼程式設計2

[root@localhost desktop]# cat shell.sh 

#!/bin/bash

#this is a shell script

if [ ! -e /media/haha ]

then

mkdir -p /media/haha

fi[root@localhost desktop]#

3、簡單指令碼程式設計3

[root@localhost desktop]# cat shell.sh 

#!/bin/bash

#this is a shell script

ping -c 3 -i 0.2 -w 3 $1 &> /dev/null

if [ $? -eq 0 ]

then

echo "host $1 is on-line"

else

echo "host $1 is off-line"

fi[root@localhost desktop]#

4、簡單的shell指令碼程式設計4

[root@localhost desktop]# cat shell.sh 

#!/bin/bash

#this is a shell script

read -p "enter : " grade

if [ $grade -ge 80 ] && [ $grade -le 100 ] ; then

echo "$grade is excellent"

elif [ $grade -ge 70 ] && [ $grade -le 84 ] ; then

echo "$grade si pass"

else

echo "$grade is fail"

fi[root@localhost desktop]#

5、簡單的shell指令碼程式設計5,批量建立使用者

[root@localhost desktop]# cat shell.sh 

#!/bin/bash

#this is a shell script

read -p "enter : " passwd

for uname in `cat user.txt`

doid $uname &> /dev/null

if [ $? -eq 0 ]

then

echo "already exists"

else

useradd $uname &> /dev/null

echo "$passwd" | passwd --stdin $uname &> /dev/null

if [ $? -eq 0 ]

then

echo "$uname create success"

else

echo "uname create failure"

fifi

done

[root@localhost desktop]#

6、簡單指令碼程式設計6,pingip位址

[root@localhost desktop]# cat shell.sh 

#!/bin/bash

#this is a shell script

for ip in `cat ip.txt`

doping -c 3 -i 0.2 -w 3 $ip &> /dev/null

if [ $? -eq 0 ] ; then

echo "host $ip is on-line"

else

echo "host $ip is off-line"

fidone

[root@localhost desktop]#

7、簡單的指令碼程式設計

[root@localhost desktop]# cat shell.sh 

#!/bin/bash

#this is a shell script

price=$(expr $ranom % 1000)

times=0

while true

doread -p "enter : " int

echo $price

echo #int

let times++

if [ $int -eq $price ] ; then

echo "okokok,$times"

exit 0

elif [ $int -gt $price ] ; then

echo "high"

else

echo "low"

fidone

[root@localhost desktop]#

8、

Shell 指令碼程式設計

1 執行shell的方法 指定shell bin sh 由sh執行指令碼 指令碼總是由sh解釋 顯示呼叫shell sh scriptname 在當前shell中執行指令碼 profile profile是可執行的 ksh profile profile是不可執行的 改變當前的執行環境責應輸入.pr...

Shell 指令碼程式設計

a file 如果 file 存在則為真。b file 如果 file 存在且是乙個塊特殊檔案則為真。c file 如果 file 存在且是乙個字特殊檔案則為真。d file 如果 file 存在且是乙個目錄則為真。e file 如果 file 存在則為真。f file 如果 file 存在且是乙個...

shell指令碼程式設計

今天看看shell程式設計,記錄下期中與想象中不一樣的地方 0.注釋用 1.shell的變數賦值 your name zhm 在your name和等號中間不能有空格,這和別的語言很不一樣,那麼在使用變數時和別的語言也不一樣,要使用 your name,一般要用 一定是大括號 2.那麼如何把乙個變數...