BASH Script Help

Silver-0-surfer

Well-Known Member
Joined
Jan 5, 2008
Messages
317
Hi Guys. I really need some help with a script Im trying to write.

I have a GNS3 server and just for grins I wanted to make a script that would open a new gnome-terminal, telnet to the console port and set the title of the shell. So I googled and found this should work

gnome-terminal --tab -e 'telnet 1.1.1.1 2000 ' --title=R0 --tab -e 'telnet 1.1.1.1 2001 ' --title=R1

this works fine, but as the labs differentiate I wanted it to be dynamic so i made(making) a small script, this is what I have

Code:
#!/bin/bash

#Define some variables
counter=0;
command="gnome-terminal "

#Get info from user
echo "Enter the amount of routers to connect to";
read amount;

echo "Enter the IP Address of Dynagen/Dynamips Server";
read ip;

#Build command
while [ "$counter" \< "$amount" ]; do
newcommand=" --tab -e  'telnet $ip 200$counter ' --title=R$counter ";
command="$command$newcommand";
counter=$[counter+1];
done

#Run command
echo $command;
$command;

(the echo $command is just used for trouble shooting)

the result is this

[admin@user Scripts]$ bash gns3.sh
Enter the amount of routers to connect to
3
Enter the IP Address of Dynagen/Dynamips Server
1.1.1.1
gnome-terminal --tab -e 'telnet 1.1.1.1 2000 ' --title=R0 --tab -e 'telnet 1.1.1.1 2001 ' --title=R1 --tab -e 'telnet 1.1.1.1 2002 ' --title=R2
Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was ''telnet')

if i run the output manually (gnome-terminal --tab -e 'telnet 1.1.1.1 2000 ' --title=R0 --tab -e 'telnet 1.1.1.1 2001 ' --title=R1 --tab -e 'telnet 1.1.1.1 2002 ' --title=R2) it works but the script ALWAYS fails with

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

any ideas?
 

biena

Executive Member
Joined
Dec 6, 2006
Messages
5,883
your ' is getting eaten by the bash shell when try to execute the gnome-terminal from within the bash shell

escape where you build up the command (added \ in front of the ')
newcommand=" --tab -e \'telnet $ip 200$counter \' --title=R$counter ";
 
Top