Problem with script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with script
# 1  
Old 02-26-2016
Problem with script

Script works fine on the server on which it is running, but fails to run the command when it runs on a remote host using the ssh.Here's the script:

Code:
#!/bin/bash

fdate=$(date "+%Y-%m-%d")
export curr_host="`hostname`.we.com"
if [ ! -s ${HOME}/hosts.txt ]
  then
  echo "Hosts file is missing or a zero-byte file. Exiting the process" >> log.txt
  exit 1
  else
 IFS=$'\n'
    for i in `dsmc q fi |sed '0,/----/d'| awk '{print $2,$3,$5}'`
  do
  echo "$curr_host $i" >> tsm_bkp_$fdate.txt
   done
 while read line <&3
  do
ssh -t $line "IFS=$'$\n';for i in `dsmc q fi |sed '0,/----/d'| awk '{print $2,$3,$5}'`; do echo "$line" $i >> ${HOME}/tsm_bkp_$fdate.txt; done"
 done 3<${HOME}/hosts.txt
fi

I am expecting the script to do the following.

1. Run the command on current host, add hostname to each line from the command output.
2. Run the command on remote host, add hostname to each line from the command output.
3. Consolidate all the information into one file located on the current server.

Below is the output file created by the script. From the log file, it is evident that the output from the command executed on the local host is written to the log file, but errors out while running on the remote host.

Code:
infadev.we.com 09/11/15 02:01:30 /
infadev.we.com 09/11/15 02:02:19 /app/infa
infadev.we.com 09/11/15 02:02:16 /app/ca
infadev.we.com 09/11/15 02:01:30 /boot
infadev.we.com 09/11/15 02:01:30 /home
infadev.we.com 09/11/15 02:01:30 /stg/bin
infadev.we.com 09/11/15 02:01:38 /opt
infadev.we.com 09/11/15 02:01:41 /usr
infadev.we.com 09/11/15 02:01:46 /var

Code:
bash: -c: line 1: syntax error near unexpected token `09/11/15'
bash: -c: line 1: `09/11/15 02:02:19 /app/infa'
Connection to infait.we.com closed.

bash: -c: line 1: syntax error near unexpected token `09/11/15'
bash: -c: line 1: `09/11/15 02:02:19 /app/infa'
Connection to infast.we.com closed.


Moderator's Comments:
Mod Comment Please wrap all code, data, input & output/errors in CODE tags.
It makes it far easier to read and preserves multiple spaces for indentation or fixed width data.

Last edited by rbatte1; 02-26-2016 at 05:06 PM.. Reason: Added CODE tags for error output
# 2  
Old 03-02-2016
Just wanted to check if someone can help.

Thank you.
# 3  
Old 03-02-2016
There's a $ char too many in the IFS definition for the remote execution. I'd propose to move the redirection to after the while loop to untangle what's done where.

Looks like the bash shell wants (and fails) to expand/execute/... 09/11/15 02:02:19 /app/infa which is the output of your dsmc |sed | awk command substitution and thus the contents of $i which in turn is ONLY referenced in the echo after the expanded $line - so I guess sth is wrong with $line - what's the structure of your {HOME}/hosts.txt file? Any unusual line terminators, for instance?

---------- Post updated at 23:31 ---------- Previous update was at 23:16 ----------

And, would replacing the entire for loop with
Code:
dsmc q fi | awk -vL=$line 'NR==1,/----/ {next} {print L, $2, $3, $5}'

work?

Last edited by RudiC; 03-02-2016 at 06:21 PM..
# 4  
Old 03-03-2016
The problem with ssh arguments is that they are processed by the local shell then again by the shell on the remote host. So one often needs to escape two times.
Therefore I recommend a script that runs on one host, plus another script that loops over all hosts, each time passing the one-host script for remote execution.
Code:
  ssh "$host" /bin/bash < local_script


Last edited by MadeInGermany; 03-03-2016 at 09:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in calling a script inside a script

Hi team, I have a script in different folder. Now i want to call that script and execute that script from that path alone. My code is #!/bin/bash wname=yahoo PATH='/opt/IBM' wac=`/usr/bin/ls $PATH | /usr/bin/grep "$wname"` STOP=`/usr/bin/find $PATH/$wac -type f -name "stop.sh"`... (8 Replies)
Discussion started by: natraj005
8 Replies

2. Shell Programming and Scripting

Shell script newbie, what is problem with my script?

Hello, Ubuntu server 11.10 can anybody help what is problem with my shell script? #!/bin/bash #script to find out currently logged on user is root or not. if ] then echo "You are super" else echo "You are awesome!" fi When I run script, I get following output ./uid: line 3: I... (4 Replies)
Discussion started by: kaustubh
4 Replies

3. Shell Programming and Scripting

Problem while calling a script within a script

Hi , I have moduled my scripts in three scripts . From First script i am calling second and from second i am calling third for some check . Problem is with the third script call. ---In second script EXP ='test.\abc.\Server.*abc.xml.*' pid=$($HOME/bin/checkpid $EXP) --Third... (2 Replies)
Discussion started by: amrishn
2 Replies

4. Shell Programming and Scripting

Problem running a program/script in the background from a script

Hi all, I have a script that calls another program/script, xxx, to run in the background. Supposedly this program at most should finish within five (5) minutes so after five (5) minutes, I run some other steps to run the script into completion. My problem is sometimes the program takes... (5 Replies)
Discussion started by: newbie_01
5 Replies

5. Shell Programming and Scripting

Problem with a script

Hi everyone, I got a problem with a script. What it's supposed to do is: to take as arguments a directory name <dir> and a dimension (in byte) <dim>; if <dir> exists, to write name and dimension of every regular file within it that sizes lesser than <dim> in regFileList. Nothing happens... (10 Replies)
Discussion started by: Luke Bonham
10 Replies

6. UNIX for Dummies Questions & Answers

Problem with script

Hello All. I have a script that is suppossed to start up a daemon but when executed, simply hangs. Could you please take a look and let me know where the problem might be? TIA ################################################################### # # SCRIPT: dstart3000.sh # Bring up the Domain... (6 Replies)
Discussion started by: grin1dan
6 Replies

7. Shell Programming and Scripting

call shell script from perl cgi script problem

hi,, i have perl scipt with line : system('./try.sh $t $d $m'); in shell scipt try.sh i have the line: echo $1 its not printing value of $t that i hav passed..y is it so..i am running it from apache web server (2 Replies)
Discussion started by: raksha.s
2 Replies

8. Shell Programming and Scripting

ssh script problem problem

Hi Please help me with the following problem with my script. The following block of code is not repeating in the while loop and exiting after searching for first message. input_file ========== host001-01 host001-02 2008-07-23 13:02:04,651 ConnectionFactory - Setting session state... (2 Replies)
Discussion started by: pcjandyala
2 Replies

9. Shell Programming and Scripting

Help. Script problem

hey guys. i have a bunch of programs in a script that needs to run as root and the rest as another user, we'll call him gabriel. now, in this script, i want to run the first few lines as root. now, how do i, after running as root, tell the script to run the remaining lines as the user gabriel?... (3 Replies)
Discussion started by: Terrible
3 Replies

10. UNIX for Dummies Questions & Answers

Problem starting a script from a 'main'-script

Please Help! :o I have a main script (ksh) where another script is called (convert_picture). Normally this works ok, but since some changes has been made on the unix-server (I dont know what :( ) suddenly it doesnt work anymore: i get an error message: ksh: convert_picture not found. I am... (3 Replies)
Discussion started by: Rakker
3 Replies
Login or Register to Ask a Question