① 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这个内联重定向数据中去。再试试看。