Pergunta

I have a file with a list of servers:

SERVERS.TXT:

    192.168.0.100
    192.168.0.101
    192.168.0.102

From a gnome terminal script, I want open a new terminal, with a tab for each server.

Here is what I tried:

gnome-terminal --profile=TabProfile `while read SERVER ; do echo "--tab -e 'ssh usr@$SERVER'"; done < SERVERS.TXT`

Here is the error:

Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was ''ssh')

Tried removing the space after the -e

gnome-terminal --profile=TabProfile `while read SERVER ; do echo "--tab -e'ssh usr@$SERVER'"; done < SERVERS.TXT`

And I get a similar error:

Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was 'usr@192.168.0.100'')

Obviously there is a parsing error since the the shell is trying to be helpful by using the spaces to predict and place delimiters. The server file is changed without notice and many different sets of servers need to be looked at.

Foi útil?

Solução

I found this question while searching for an answer to the issue the OP had, but my issue was a little different. I knew the list of servers, they where not in a file.

Anyway, the other solutions posted did not work for me, but the following script does work, and is what I use to get around the "--command/-e" is not a valid command" error.

The script should be very easy change to suit any need:

#!/bin/sh
# Open a terminal to each of the servers
#
# The list of servers
LIST="server1.info server2.info server3.info server4.info"
cmdssh=`which ssh`

for s in $LIST
do
    title=`echo -n "${s}" | sed 's/^\(.\)/\U\1/'`
    args="${args} --tab --title=\"$title\" --command=\"${cmdssh} ${s}.com\""
done

tmpfile=`mktemp`
echo "gnome-terminal${args}" > $tmpfile
chmod 744 $tmpfile
. $tmpfile
rm $tmpfile

Now the big question is why does this work when run from a file, but not from within a script. Sure, the issue is about the escaping of the --command part, but everything I tried failed unless exported to a temp file.

Outras dicas

I would try something like:

$ while read SERVER;do echo -n "--tab -e 'ssh usr@$SERVER' "; \
  done < SERVERS.txt | xargs gnome-terminal --profile=TabProfile

This is to avoid any interpretation that the shell could do of the parameters (anything starting with a dash).

Because it is concatenating strings (using -n), it is necessary to add an space between them.

Is this a problem of parsing command-line options? Sometimes if you have one command sending arguments to another command, the first can get confused. The convention is to use a -- like so:

echo -- "--tab -e 'ssh usr@$SERVER'";

Try to type

eval

before gnome terminal command.

it should be something like this:

eval /usr/bin/gnome-terminal $xargs

worked for me!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top