Loop breaks through ssh in script


 
Thread Tools Search this Thread
Operating Systems AIX Loop breaks through ssh in script
# 1  
Old 11-18-2013
Loop breaks through ssh in script

hello all,

i have an AIX6.1 machine and INFORMIX 11.7 database server.
i have a script to create users on 3 machines and also i need to grant this user access to a specific database. the script works and it does what i want it to do but the loop doesnt work. for example if i insert 10 lines in my input file to create 10 different users it only executes the first line. i suspect that this should have something to do with ssh cause i really cannot find any other explanation.
here is the script:
Code:
while read SEMGROUP SEMBANKDIR COOPUSER DBNAME;
do
 echo " $a Creating user for server machine1 $b "
 if `sudo mkuser pgrp=$SEMGROUP groups="$SEMGROUP" su='false' home=/$SEMBANKDIR/login/$COOPUSER pwdwarntime='3' histexpire='8' maxage='4' minlen='8' minalpha='2' minother='2' maxrepeats='2' gecos=$COOPUSER umask='002' $COOPUSER`
 then
    echo $COOPUSER:$COOPUSER |sudo chpasswd
    cd /$SEMBANKDIR/login/
    sudo chmod -R 771 $COOPUSER
    echo " $a User $COOPUSER created successfully on machine1 $b "
 else
    echo " $a Problem in creating user $COOPUSER on machine1 $b "
 fi

echo "------------------------------------------------------------------"

 echo " $a Creating user for server machine2 $b "
 if `sudo ssh machine2 sudo mkuser pgrp="$SEMGROUP" groups="$SEMGROUP,bank" su='false' login='false' rlogin='false' home=/sem/login/deltausers pwdwarntime='3' histexpire='8' maxage='4' minlen='8' minalpha='2' minother='2' maxrepeats='2' gecos=$COOPUSER umask='002' $COOPUSER`
 then
    sudo ssh machine2 echo $COOPUSER:$COOPUSER |sudo chpasswd
    cd /sem/login/informix/
    $INFORMIXDIR/bin/dbaccess $DBNAME@tcp_semids << q
grant connect to '$COOPUSER';
grant dbpermissions to '$COOPUSER';
grant DEFAULT ROLE dbpermissions to '$COOPUSER';
q
    cd -
    echo " $a User $COOPUSER created successfully on machine2 $b "
 else
    echo " $a Problem in creating user $COOPUSER on machine2 $b "
 fi

echo "---------------------------------------------------------------------"

 echo " $a Creating user for server machine3 $b "
 if `sudo ssh machine3 sudo mkuser pgrp=$SEMGROUP groups="$SEMGROUP" su='false' home=/$SEMBANKDIR/login/$COOPUSER pwdwarntime='3' histexpire='8' maxage='4' minlen='8' minalpha='2' minother='2' maxrepeats='2' gecos=$COOPUSER umask='002' $COOPUSER`
 then
    sudo ssh machine3 echo $COOPUSER:$COOPUSER |sudo chpasswd
    sudo ssh machine3 sudo chmod -R 771 /$SEMBANKDIR/login/$COOPUSER
    echo " $a User $COOPUSER created successfully on machine3 $b "
 else
    echo " $a Problem in creating user $COOPUSER on machine3 $b "
 fi

done

i cannot figure out why the loop is not executed.
any ideas?

thank you
# 2  
Old 11-18-2013
It's the usual problem that ssh reads from stdin - the while-loop's input.
The usual measure: add -n option to ssh!
--
I wonder what the backticks in the if clause are good for - can't you simply omit them?
The risk of the backticks: an arbitrary message on stdout is run as a command.
# 3  
Old 11-18-2013
thank you i will try the -n option to see if it works.
i use the backticks because this way i tell the script if the command inside the backticks executes successfully then proceed. else if the command fails it doesnt get into the if statement.
# 4  
Old 11-18-2013
Quote:
Originally Posted by omonoiatis9
thank you i will try the -n option to see if it works.
i use the backticks because this way i tell the script if the command inside the backticks executes successfully then proceed. else if the command fails it doesnt get into the if statement.
That's nonsense. The exit status of the command between if and then is always tested - you don't need backticks for that.
# 5  
Old 11-18-2013
i didnt know that.
by the way i tested the script with -n and the loop works!
thanks
# 6  
Old 11-18-2013
Quote:
Originally Posted by omonoiatis9
i use the backticks because this way i tell the script if the command inside the backticks executes successfully then proceed. else if the command fails it doesnt get into the if statement.
No, no and again: no.

First, the "if"-statement:

The if-statement is defined as:
Code:
if command ; then commandA ; else commandB ; fi

command can be any command, simple or complex. In either way its return code is used to determine if the then-branch or the else-branch is executed. If the return code ("$?") is 0 (zero), the then-branch is executed, otherwise the else-branch.

The probably most often-used command in this position is /usr/bin/test or its counterpart /usr/bin/[. Both work the same way (the difference being that "[" requires "]" as the last argument): if the tests specified as arguments add up to a boolean TRUE it returns 0, for a boolean FALSE it returns 1.

Still, the same way as you use test you can use any other command. For instance:

Code:
if grep -q EXPR /some/file ; then

will execute the then-branch if EXPR is found in /some/file and the else-branch if not, because grep will return 0 if it finds what is searched for and 1 if it doesn't.

Second: the backticks.

Even if you need process substitution (which isn't the case, see above), you should NOT use backticks - not the last 20 years or so. Even ksh88 already had "$(...)" instead of "`...`", which is not only nestable, but also the POSIX-compatible way to enclose a process. Backticks are supported only for one reason: backwards compatibility. There is absolutely no reason to use them any more, because they offer absolutely no advantage over "$(...)", only drawbacks.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop is causing ssh command to exit from script after first iteration.

I am trying to check multiple server's "uptime" in a loop over "ssh". When I execute multiple ssh commands with hard coded servernames script is executing fine. But when I pass server names using while loop, script is exiting after checking first server's status, why? # serverList... (8 Replies)
Discussion started by: kchinnam
8 Replies

2. UNIX for Dummies Questions & Answers

Page breaks and line breaks

Hi All, Need an urgent solution to an issue . We have created a ksh file or shell script which generates 1 DAT file. the DAT file contains extract of a select statement . Now the issue is , when we are executing the ksh file , the output is coimng with page breaks and line breaks . We have... (4 Replies)
Discussion started by: Ayaskant
4 Replies

3. Shell Programming and Scripting

ksh while read loop breaks after one record - AIX

#!/bin/ksh for SRV in imawasp01 \ imawasp02 \ imawasp03 \ imawasp04 \ imawasp05 \ imawasp06 \ imawasp07 \ imawasp08 \ imawasp09 do print "${SRV}" while read PASSLINE do SRVNAME=`echo ${PASSLINE} | awk -F\: '{print $1}'` LASTLOGIN=`ssh ${SRV} lsuser ${SRVNAME} | tr '... (2 Replies)
Discussion started by: port43
2 Replies

4. Shell Programming and Scripting

Loop breaks on yes/no user input

I have a shell script, and its pretty much done, I decided to add a loop that ends or continues depending on user input. like "would you like to continue?" and if I hit y or yes it will run the loop again until I hit n or no and breaks out of the loop. To be hones I didn't think I needed to add... (2 Replies)
Discussion started by: Demon_Jester
2 Replies

5. Shell Programming and Scripting

Remove line breaks in csv file using shell script

Hi All, I've a csv file in which the record is getting break into 1 line or more than one line. I want to combine those splits into one line and remove the unwanted character existing in the record i.e. double quote symbol ("). The line gets break only when the record contains double... (4 Replies)
Discussion started by: rajak.net
4 Replies

6. Shell Programming and Scripting

Need help on ssh usage in a loop of shell script

I need help on how to connect remote systems through ssh command in while loop of shell script. I was able to connect one remote system using ssh from shell script. Please find sample code snippet as given below….. ssh "root@148.147.179.100" ARG1=$rpmFileName 'bash -s' <<'ENDSSH' ... (5 Replies)
Discussion started by: rajesh.tulluri
5 Replies

7. Shell Programming and Scripting

ssh breaks loop

Here is the smallest extract to demonstrate the problem that I experience. #!/bin/bash r=$1 while read ip do if ] ;then x=`ssh $ip echo "$ip"` else x=`echo "$ip"` fi echo $x done << EOF 192.168.8.241 192.168.8.241 EOF # Any IP with public key set (0 Replies)
Discussion started by: ivolvo
0 Replies

8. UNIX for Dummies Questions & Answers

ssh and for loop

Hi all, I am facing an issue while trying to access a for loop variable inside ssh. Can anyone please help me with what this issue is, the following is the code that I have -> IMPL_LOG_FOLDERS=(transaction_logs invalid_transaction_logs) sshg3 ftp_id@boxname << EOS for log_folder in... (5 Replies)
Discussion started by: anindyabecs
5 Replies

9. Shell Programming and Scripting

using ssh with a for loop

How do you do an ssh on a for statement. I have done ssh on individula lines of code before with no problems. #!/usr/bin/ksh cat server_list.txt | while read line do ssh $line "for i in `lslpp -l |grep tsm` do lslpp -Lc $i |grep -v State |tr ':' ',' |awk -F, '{print $2, "," ,... (4 Replies)
Discussion started by: din
4 Replies

10. Shell Programming and Scripting

PHP Script that sends mail - Postfix breaks it

I have a PHP Script that works perfectly on a server that uses Sendmail. However I tried to port it to a new server that has the Postfix to Sendmail compatibility interface, and it doesn't work properly. The php.ini file has the path for sendmail on both servers set to: /usr/sbin/sendmail -t... (0 Replies)
Discussion started by: boopfm523
0 Replies
Login or Register to Ask a Question