LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help with simple shell scripting (http://www.linuxquestions.org/questions/linux-newbie-8/help-with-simple-shell-scripting-878266/)

choconlangthang 05-02-2011 02:13 AM

help with simple shell scripting
 
Hi,
I'm a Linux newbie, pls help me with this. I want a simple for loop to open N terminal windows (supposed to be input at runtime), I coded this way

Code:

#!/bin/bash
LIMIT = 10 #hard-coded since I dont know how to take input on the fly

for ((a = 1 ; a<= LIMIT; a++))
do
  gnome-terminal -e "ssh -X user@blablaserver.com"
done

exit 0

It always got error "command not found", could anyone help pls? tks

colucix 05-02-2011 02:19 AM

The complete error message should reveal the solution:
Code:

line 2: LIMIT: command not found
the word not recognized as a command is LIMIT. Indeed when you assign a variable, you should pay attention to the extra blank spaces: actually they must not be there, that is the equal sign should be immediately after the variable name without spaces before and after:
Code:

LIMIT=10
Regarding the input from the user, try the read statement, e.g:
Code:

read -p "How many terminal sessions? " ans
for ((a = 1 ; a <= ans; a++))
do
  gnome-terminal -e "ssh -X user@blablaserver.com"
done

Hope this helps.

choconlangthang 05-02-2011 02:30 AM

Quote:

Originally Posted by colucix (Post 4343554)
The complete error message should reveal the solution:
Code:

line 2: LIMIT: command not found
the word not recognized as a command is LIMIT. Indeed when you assign a variable, you should pay attention to the extra blank spaces: actually they must not be there, that is the equal sign should be immediately after the variable name without spaces before and after:
Code:

LIMIT=10
Regarding the input from the user, try the read statement, e.g:
Code:

read -p "How many terminal sessions? " ans
for ((a = 1 ; a <= ans; a++))
do
  gnome-terminal -e "ssh -X user@blablaserver.com"
done

Hope this helps.

Sublime answer, works like a champ, tks.

For the knowledge, space doesn't affect anything in the for loop, I can put either
Code:

for ((a = 1 ; a <= ans; a++))

or
for ((a=1 ; a<= ans; a++))

and it still works (?)

And I bet the best thing to ask is you have a link to a practical shell scripting for user (not admin)? :)

colucix 05-02-2011 02:42 AM

Yes. It is a specific syntax of the for loop, that resembles the C loops and since in C there is not such a limitation (with or without spaces is the same) you are free to put them or not inside the double parentheses construct.

To learn shell scripting you can check:
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html (PDF available)
http://www.tldp.org/LDP/abs/html/index.html (PDF available)
For the command line in general:
http://www.linuxcommand.org/tlcl.php


All times are GMT -5. The time now is 11:01 PM.