1

I'm trying to check if a particular line exists in filename on a host. So only if the particular pattern is found from the grep command a dummy.txt will be created. Then I want to check if dummy.txt exists as a condition and proceed.

But even though dummy.txt is getting created its not being found by the if statement :

if {[file exists $fileName] }

and the control is transferred to the else block. What could be the reason? Is there any easier method to achieve what I'm trying to ?

 set Host [lindex $argv 0 ]
 spawn ssh -o StrictHostKeyChecking=no userid@$Host
 expect "*assword*"
 send "password\n"
 expect "+"
 send "cd path/that/contains/filename\n"
 expect "*$*"
 send "pwd\n"
 set fileName dummy.txt
 send {grep '\[abc\]' filename | wc -l | { read line; [ "$line" != "0" ] && echo "$line"> dummy.txt; }}
 send "\n"
 expect "+"
 send {grep 'xyz' filename | wc -l | { read line; [ "$line" != "0" ] && echo "$line"> dummy.txt; }}
 send "\n"
 expect "+"
 send "chmod 777 dummy.txt\n"
 if {[file exists $fileName] } {

   send {printf "\nabc xyz already exists"} 
   send "\n"
   expect "+"
   send "ls -l\n"
   expect "+"
   send "rm dummy.txt\n"
   expect "+"
   send "ls -l\n"
   expect "+"
        send "exit\n"
  } else {
        send {printf "cannot find $fileName"}
   send "\n"
   expect "+"
   send {printf "\n[abc]\nxyz\n" >> filename}
   send "\n"
   expect "+"
   send "cat filename\n"
   expect "+"
    send "exit\n"

    }
user417721
  • 41
  • 1
  • 5
  • Instead of this `wc | read ...` you could simply check grep's return code (0 = match found, see `man grep`). – ott-- Feb 12 '15 at 17:51

1 Answers1

1

if {[file exists $fileName] } will check whether the file exists on the local system (the one where expect is running), but the conditionally created file will be on the remote host (the one that the script logs into with ssh) if it exists.  Also, filename is dummy, but the conditionally created file is dummy.txt.

  • Sorry scott that was a typo. Its dummy.txt everywhere. If its checking on the remote machine then how do I get it to check on the host ? I'm trying to automate the process of adding particular lines to a file, but at the same time if those lines already exist I don't want duplicate entries, which is why I'm using grep and dummy.txt – user417721 Feb 12 '15 at 01:45
  • Thanks for fixing the typo in the question. But, as long as you're editing the question, you should make it clearer. I don't fully understand your comment. If you want to do something conditionally on the remote host based on condition(s) on the remote host, just think of it as a shell script and use shell logic; e.g., `send {grep -q xyz filename || echo xyz >> filename}`. – Scott - Слава Україні Feb 12 '15 at 03:12
  • I need to add some content say, xyz abc to the file filename on the remote host. But I want to check if those xyz abc already exists in filename. to check this I'm using send {grep '\[abc\]' filename | wc -l | { read line; [ "$line" != "0" ] && echo "$line"> dummy.txt; }} send "\n" expect "+" send {grep 'xyz' filename | wc -l | { read line; [ "$line" != "0" ] && echo "$line"> dummy.txt; }} send "\n" So if xyz abc exists in filename a new file dummy.txt is created. So if dummy.txt exists on the remote that'll mean xyz and abc are present in filename and the script need not add them again – user417721 Feb 12 '15 at 03:29
  • I know its seems a bit lengthy but I couldn't think of a simpler way to accomplish this since I've to use interaction. If you can suggest a solution to check for the file on the remote host or a simpler way to make this entire thing work, it'd be great :) – user417721 Feb 12 '15 at 03:34
  • `send {[ -f dummy.txt ] && echo "xyz abc" >> filename}`. Or read my most recent comment again. – Scott - Слава Україні Feb 12 '15 at 03:50
  • Hi Scott, thanks ! But I don't seem to understand how or where this would in my expect script. Could you please explain ? – user417721 Feb 12 '15 at 05:56
  • What do you not understand? You're already doing stuff on the remote host conditionally by sending `[…] && …`. Just adapt that to do what you want -- as I illustrated in my last comment. – Scott - Слава Україні Feb 12 '15 at 06:29