简体   繁体   English

从脚本中打开新的gnome-terminal,并从当前脚本中输入var。

[英]Open new gnome-terminal from scripts and input vars from present script.

#!/bin/bash

Dpath=/home/$USER/Docker/
IP=`sed -n 1p /home/medma/.medmadoc`
DockerMachine=`sed -n 2p /home/$USER/.medmadoc`
DockerPort=`sed -n 5p /home/$USER/.medmadoc`
DockerUser=`sed -n 3p /home/$USER/.medmadoc`
DockerPass=`sed -n 4p /home/$USER/.medmadoc`

if [ ! -d $Dpath ] ; then
mkdir -p $Dpath
else 
stat=`wget -O ".dockerid" http://$IP/DOCKER-STAT.txt`
for ids in `cat .dockerid`
do
if [ "$ids" == "$DockerMachine" ] ; then
gnome-terminal -x sh -c 'sshfs -p$DockerPort $DockerUser@$IP:/var/www/html $Dpath ; bash '
nautilus $Dpath
zenity --info --text "Mounted $DockerMachine"
exit
else
:
fi
done
zenity --info --text "No Such ID:$DockerMachine"
fi

gnome-terminal -x sh -c 'sshfs -p$DockerPort $DockerUser@$IP:/var/www/html $Dpath ; gnome-terminal -x sh -c'sshfs -p $ DockerPort $ DockerUser @ $ IP:/ var / www / html $ Dpath; bash '

this command opens up a new terminal but the problem is that it does not load vars like $DockerPort $DockerUser $IP $Dpath from this script. 这个命令打开了一个新的终端,但是问题是它不会从该脚本中加载$ DockerPort $ DockerUser $ IP $ Dpath这样的变量。

How do I input the values in these vars from this script to the newly opened terminal ? 如何将这些var中的值从此脚本输入到新打开的终端?

Thanks ! 谢谢 !

As indicated before, you could try to use double quotes instead of single quotes around the sshfs invocation. 如前所述,您可以尝试在sshfs调用周围使用双引号而不是单引号。

Single quotes in Bash are used to delimit verbatim text, in which variables are not expanded. Bash中的单引号用于分隔逐字记录文本,其中的变量不会扩展。 Double quotes, in contrast, allow for variables expansion and command substitution ( $(...) ) to take place. 相反,双引号允许变量扩展和命令替换( $(...) )发生。

If you do use double quotes, beware of unintended side-effects (your username may contain a space, a dollar, a semicolon, or any other shell-special character). 如果确实使用双引号,请当心意外的副作用(您的用户名可能包含空格,美元,分号或任何其他shell特殊字符)。 A cleaner approach would be to export the variables to the environment before calling gnome-terminal (and not forgetting to add double quotes around your variables inside the single-quotes), so that your code looks like : 一种更干净的方法是在调用gnome-terminal之前将变量导出到环境中(并且不要忘记在单引号的变量周围添加双引号),这样您的代码看起来像:

export Docker{Port,User} IP Dpath
gnome-terminal -x sh -c 'sshfs -p"$DockerPort" "$DockerUser@$IP":/var/www/html "$Dpath" ; bash'

You may not want to pollute the environment with variables that will only be used once. 您可能不想使用只会使用一次的变量来污染环境。 If that is the case, instead of export ing them, you can use Bash's declare -p feature to serialize variables before loading them into a new environment (in my opinion, this is the cleanest approach). 如果是这种情况,您可以使用Bash的declare -p功能来序列化变量,然后再将其加载到新环境中(我认为这是最干净的方法),而不是export它们。 Here is what it looks like : 这是它的样子:

set_vars="$(declare -p Docker{Port,User} IP Dpath)"
gnome-terminal -x bash -c "$set_vars;"'sshfs ....'

Using this latest method, the variables are only visible to the shell process that runs the sshfs command, not gnome-terminal itself nor any sub-process run thereafter. 使用这种最新方法,这些变量仅对运行sshfs命令的shell进程可见,而对gnome-terminal本身以及此后运行的任何子进程都不可见。

PS: you could read all your variables at once from the ~/.medmadoc file by using the following code instead of repeated sed invocations : PS:您可以使用以下代码而不是重复进行sed调用,从〜/ .medmadoc文件中一次读取所有变量:

for var in IP Docker{Machine,User,Pass,Port}; do 
  read $var
done < ~/.medmadoc

This code makes use of the read builtin, that reads a line of input into a variable (in its simplest form). 该代码利用read内置函数,该函数将输入的一行读入变量(以最简单的形式)。

PPS: That stat variable probably won't contain any useful information, since the output of wget was redirected by the -O flag. PPS:该stat变量可能不会包含任何有用的信息,因为wget的输出已通过-O标志重定向。 Perhaps you meant to store the result code of wget into stat, in which case what you meant was : 也许您打算将wget的结果代码存储到stat中,在这种情况下,您的意思是:

wget -O .dockerid ...
stat=$?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM