Linux shell指令碼作業

2021-07-28 02:58:57 字數 1709 閱讀 8024

1)要求讀取/mnt/username使用者檔案和/mnt/password密碼檔案,並判斷檔案是否存在,若都存在則判斷其行數是否一致.

2)要建立的使用者是否存在,若存在則顯示存在不再建立,若不存在則建立

滿足以上條件建立使用者

效果圖

**如下

#!/bin/bash

userfile="/mnt/username"

passwdfile="/mnt/password"

usernum=`wc -l /mnt/username |cut -d

" "-f

1`passwdnum=`wc -l /mnt/password |cut -d

" "-f

1`if

[ -f

$userfile ]

then

if [ -f

$passwdfile ]

then

if [ $

-eq$ ]

then

for n in $(seq 1

$usernum )

dousername=`sed -n $p /mnt/username`

password=`sed -n $p /mnt/password`

id $username >&/dev/null

if[ $? -ne

0 ] then

useradd $username >&/dev/null

echo

$password | passwd --stdin $username >&/dev/null

echo

"$username 建立成功"

else

echo

"$username 已經存在"

fidone

else

echo 使用者名稱與密碼個數不匹配

fielse

echo 密碼檔案不存在

fielse

echo 使用者檔案不存在

fi

要求直接執行(指令碼 使用者 密碼 ip)連線ssh

效果圖

**如下

ssh

#!/usr/bin/expect

set user [ lindex $argv

0 ]set passwd [ lindex $argv

1 ]set ip [ lindex $argv

2 ]spawn /mnt/ssh

expect

"yes"

"please ip"

"password"

expect eof

}

answer.exp

#!/bin/bash

read -p "please user"

-s user

read -p "please ip"

-s ip

ssh $@$

Linux Shell指令碼基礎

shell指令碼在處理自動迴圈或大的任務方面可節省大量時間,且功能強大。任何指令碼都可能有注釋,加注釋需要此行的第乙個字元為 直譯器對此行不予解釋。指令碼不是複雜的程式,它是按行解釋的。指令碼第一行總是以 bin sh開始,這段指令碼通知shell使用系統上的 bourne shell直譯器。指令碼...

linux shell指令碼犯錯

指令碼裡犯了錯 oracle it shell value 1 oracle it shell value 1 bash value command not found oracle it shell value 3 bash value command not found oracle it sh...

Linux shell 指令碼例項

1.寫乙個指令碼,利用迴圈計算10的階乘 2.寫乙個指令碼,執行後,列印一行提示 please input a number 要求使用者輸入數值,然後列印出該數值,然後再次要求使用者輸入數值。直到使用者輸入 end 停止 執行指令碼方法 nuhup sh route.sh 注意前面要用上nohup,...