0

I want to check if files count is 0 on Remote server using ssh.

Below code is only checking if file (ending with .dmp exists )

if ssh v0021x91 ls /apps/oracle/home/DB_1201/*.dmp
    then
      echo "Files still exist after cleanup"
      exit -9991
    else
      echo "Files cleaned up"
   fi

For below "then" part is executed with $? =0 when actual error "ls: cannot access /apps/oracle/home/DB_1201/*.dmp: No such file or directory"

if [ `ssh v0021x91 ls /apps/oracle/home/DB_1201/*.dmp | wc -l` -eq 0 ]
   then
      echo  "[INFO  ] Dumps Files Cleaned Up on Remote Host"
    else
      echo  "[ERROR ] issue in Remote Host"
      exit -9992
fi  

I need output of "ls /apps/oracle/home/DB_1201/*.dmp |wc -l" 0 or N number. And if directory is not there (No such file or directory).

Need to return file count 0 or 28 and/or capture directory not found error.

Please advise on the correct approach of executing the SCP and after that validating its status?

Below always returns zero even if some error as its taking local server last command status.

   scp -rp /apps/oracle/home/DB_1201/*.dmp      oracle@v0021x91:/apps/oracle/home/DB_1201/
   if [ $? != 0 ]
    then
     echo -e "[ERROR ] Can't copy files from Source to Remote"
     exit -9995
   fi
  
   if    scp -rp /apps/oracle/home/DB_1201/*dmp oracle@v0021x91:/apps/oracle/home/DB_1201/
   then
     echo -e "[INFO ] SCP Success"
      else
     echo -e "[ERROR ] Can't copy files from Source to Remote"
     exit -9995
   fi

Regards, Veera

0

Your exit codes like -9991 are meaningless in any kind of script. They will be truncated modulo 256, and be returned as positive in the range 0-255. Values 128 and above have a conventional meaning (terminated by signal), and values from about 124 to 127 are used for shell process errors. You also cannot reliably use a status code for a counter.

ssh and scp return the status of themselves -- is the remote system reachable, were your credentials ok, is the remote service running? They have no mechanism to return the status of remote commands. You need to pass any information (such as outcome of the remote script) back as data returned by the remote command, and therefore identifiable as not being actual data: like STATUS::9991 if that's your thing.

If you want to capture stderr of the remote command, you probably want to redirect it 2>&1 within the ssh command.

| improve this answer | |
0
  1. if [ `ssh v0021x91 ls /apps/oracle/home/DB_1201/*.dmp | wc -l` -eq 0 ]

    The evaluation order is such that the wildcard is evaluated before the ssh command is executed. If /apps/oracle/home/DB_1202/*.dmp matches one or more files on the local host, it will be replaced with the name(s) of those file(s). Otherwise, the asterisk will remain and be passed to the remote side as literal.

    The remote shell will then evaluate the pattern before executing the ls command, either expanding it to one or more matching files or leaving it as a literal asterisk.

    If there are no matching files, ls will complain with an error that it cannot find the file /apps/oracle/home/DB_1201/*.dmp (complete with a literal asterisk as the first character of the file name). As a side effect it will output no files to wc -l and you'll get zero as the expected and correct result there.

    Finally, the test will match either zero files along with an error written to stderr, or some non-zero number of files.

    As a point of modern coding, consider replacing [ `command` ] with [ "$(command)" ]. Or, if you're writing for bash, [[ "$(command)" ]]

  2. exit -9991

    Exit codes should either be 0 (success, ok) or in the range 1-127. Using large or negative numbers is erroneous and misleading

  3. if [ $? != 0 ]

    You're using a string comparison for numeric values. In practical terms it's probably ok, but you should use [ $? -ne 0 ] or [ $? -gt 0 ] instead.

  4. if scp -rp /apps/oracle/home/DB_1201/*dmp oracle @ v0021x91:/apps/oracle/home/DB_1201/*dmp

    • What's with the spaces in the target component?
    • Don't specify wildcards in the target unless you really really know what you're doing

    It should almost certainly be oracle@v0021x91:/apps/oracle/home/dB_1202/

  5. if ssh v0021x91 ls /apps/oracle/home/DB_1201/*.dmp

    Similar to #1, you'll get an error if there are no files matching *.dmp on the remote machine, and unexpected problems if there are one or more files matching that path on the local machine.

To avoid expansion of wildcards on the local system, quote the command you're passing to ssh. So for #5 you should write if ssh v0021x91 'ls /apps/oracle/home/DB_1201/*.dmp', etc.

| improve this answer | |
  • Hi, Apologize, I have corrected the typo's for #4. – Veera V Aug 22 at 1:20
0

The only thing wrong with your first code snippet is that single quotation marks need to be put around the remote 'ls' command to delay execution of the glob pattern. Your basic approach is sound; according to the SSH manpage the exit status of the ssh command is the exit code of the remote command.

Also, exit status is limited to the range 0 - 255

Try this:

if ssh v0021x91 'ls /apps/oracle/home/DB_1201/*.dmp'
    then
      echo "Files still exist after cleanup"
      exit 1
    else
      echo "Files cleaned up"
   fi
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.