① linux 中怎樣執行腳本
首先腳本需要有執行許可權
chmo+xfile.sh
執行腳本有三種方法:
1../file.sh
特點:開啟bash子進程來執行,也就是開啟額外的進程來進行,不影響原進程的變數、配置等
2.bashfile.sh
特點:和./file.sh相同
3.sourcefile.sh或者.file.sh
特點:在原bash進程中執行腳本。
第三種方法主要用於在腳本中切換用戶su、切換目錄cd等命令。
source和.命令是相同的。
你可以搜索source
補充,如何查看腳本運行是否開啟了bash子進程
vim file.sh
寫入
#!/bin/bash
#echo $$命令會輸出bash進程ID
echo $$
保存並賦予可執行許可權chmod u+x file.sh
在你的shell中輸入,echo $$ 屏幕輸出4176
./file.sh 屏幕輸出3600
bash file.sh 屏幕輸出3984
source file.sh 屏幕輸出4176 和 你直接在shell中輸出的一樣,說明是在同一個bash進程
② linux 如何自動遠程執行腳本
1、安裝sshpass
2、sshpass -p 「passwd」 ssh -p22 root@$dst_ip 「a.sh」
注意a.sh是目標主機里的腳本。
③ linux下如何使用ssh遠程登錄主機 執行shell腳本
知道linux的ip,用戶和密碼就可以遠程登陸了。在你的SSH 客戶端會有一個linux的終端。在這執行命令就可以了。
④ shell腳本 ,在linux 下運行一個shell腳本登陸遠程unix 伺服器,請問這個腳本如何寫
#!/bin/bash
tmptty=`tty`
tmptty=`basename $tmptty`
tmpname=`whoami`
ip="xxx" #目標主機地址
inp1="xxx^M" #主機的用戶名,,注意必須有^M
inp2="xxx^M" #主機的密碼,注意必須有^M
inp3="ls^M"
inp4="pwd^M"
inputfile=in
outputfile=out.log
rm -fr $inputfile
rm -fr $outputfile
mknod $inputfile p
touch $outputfile
#file description 7 for out and 8 for in
exec 7<>$outputfile
exec 8<>$inputfile
telnet $ip <&8 >&7 &
sleep 2; echo $inp1 >> $inputfile
sleep 2; echo $inp2 >> $inputfile
sleep 2; echo $inp3 >> $inputfile
sleep 2; echo $inp4 >> $inputfile
tail -f $outputfile &
while true
do
read str
if [[ $str = "quit" || $str = "exit" ]]
then echo $str >> $inputfile exit
else echo $str >> $inputfile
fi
done
ps -ef | grep telnet | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh
ps -ef | grep tail | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh
⑤ linux遠程登陸的shell腳本for循環無結果
我比較認同「ssh之後是遠程主機執行的命令
本地變數不起作用」這種說法
你的腳本之所以$aaa能回顯,是因為在本地定義了這個變數,但本地沒有定義$i這個變數,所以無法顯示變數值,把aaa="u1 u2"一塊塞<<ff.......ff這個內聯重定向數據中去。再試試看。