How to make SFTP cozbatch return different error codes - unix

I need to get different SFTP exit codes for each error. For instance 'no such file or directory' --> exit code=552 or 550 instead of returning 1.
I've tried the following and it did not work:
//A05FTP EXEC PROC=SFTPROC,COND=(0,NE)
//COPSFTP.MYSTDIN DD *
host="xpto.xpty.xptz"
lzopts mode=text
cd /home/apl/files/unl
ls
a=`ls | wc -l`
echo `$a`
echo $?
QUIT
//*
and the output in spool is:
cozsftp> lzopts mode=text
mode=text
cozsftp> lzopts mode=text
mode=text
cozsftp> cd /home/apl/files/unl
Ý09.807¨ Invalid command.
cozsftp> a= 1
CoZBatchÝI¨: returning rc=exitcode=1
Can anyone help me?

COZBATCH allows you to embed shell scripts into JCL, so you don't need to use BPXBATCH. BPXBATCH really is a poor utility. If you're using Co:Z then good for you it rocks.
If you want to run shell commands you need to use the ! escape character.
!echo $a
FWIW, SFTP always returns 1 on error. I'm not sure if you can change that. Errors should be logged in the sysout.

Your problem may simply be the echo `$a`. Try enclosing with quotes instead of tick marks.
More generally, if you want to do more detailed error checking, instead of using the SFTP procedure (SFTPROC), I think you'd do better to write yourself a simple script that you execute with BPXBATCH. The script would issue the same SFTP commands, but you could capture and redirect the output (STDOUT/STDERR) and based on the return value ($?) and any error messages, you could certainly detect all the unusual conditions you might want.

Related

How to use the following command from R: echo "${pipestatus[1]}"?

I need to check the exit status of a piped command from R on Debian, like here, but cannot make run echo "${pipestatus[1]}" successfully from R using system2/system function. The command works properly when I use command line.
The command I am trying to use in R can look like this (the shell I use is zsh):
system2("false", args = "|true;echo '${pipestatus[1]}'")
After some testing I can see that the exit status checking command cannot be quoted properly but I cannot figure out the correct way to do so.
Am I right that quoting this command properly is the issue? How to run this (echo "${pipestatus[1]}") command from R? Are there any alternatives to using the command in question to check exit status?
You can’t use zsh features here, since system2 doesn’t invoke a shell.
Instead, you’ll either need to use a raw system call or, better, explicitly invoke the shell in system2. You’ll also need to use double quotes instead of single quotes around ${pipestatus[1]} to allow expansion — otherwise zsh will interpret it as a literal string.
system2('zsh', c('-c', shQuote('false|true; echo "${pipestatus[1]}"')))

shell command # can't be carry out when it not used for comments on colab

I'm confused about this code! Why # cant't play a role that takes the length of a string?
string="abcd"
!echo ${#string}
In fact, the code behind # has become commented and cannot be executed!
Any advice?
This works correctly, but you cannot mix python and bash variables in this way. Try this instead:
!string="abcd" && echo ${#string}
The two statements have to be on the same line because in IPython, each ! statement opens a temporary subshell and variables are not persisted between shells. If you want to use multiline bash programs, you can use the %%bash cell magic instead:
%%bash
string="abcd"
echo ${#string}

Unix: Unexpected behavior of grep command with regex search

I have a grep command that I run daily to find an entry in a huge logfile.
This command works fine in our Development environment. But in our Production environment, it outputs a response that is different from the entry in the logfile.
Here's the command:
logentry=$(grep -m1 -oP '.*(?<=Reset\s).*' $log)
Actual entry in log file:
******Reset Counter:[Total:1849766] [Success:1849766] [Insert:102] [Update:1848861] [Delete:803] [Key:0]
Command output:
******Reset Counter:[Total:1849766] 1 [Insert:102] [Update:1848861] [Delete:803] [Key:0]
Expected output:
******Reset Counter:[Total:1849766] [Success:1849766] [Insert:102] [Update:1848861] [Delete:803] [Key:0]
What could be the reason behind this inconsistent behavior of the grep command?
Thanks #Ed Morton for comment. The fix is working fine.
Root cause:
The variable is not quoted so it's open to globbing, word-splitting, and filename expansion and so the net result will be dependent on files in your directory.
Solution: Use echo "$logentry" instead and ALWAYS quote your shell variabes unless you have a specific purpose in mind by not doing so and fully understand all of the implications.
Security implications of forgetting to quote a variable in bash/POSIX shells

unix sh script - read from file

I'm trying to read the content from a aux file, but I can't figure why the command don't work, if I use the string in parameter, that was read from read from file..
Script
file=servers.aux
for server in $(cat $file)
do
echo $server
echo $server
`/usr/IBM/WebSphere/App/profiles/BPM/bin/serverStatus.sh $server -username adm -password adm`
done
Result
BPM.AppTarget.bpm01.0
ServersStatus[7]: ADMU0116I:: not found.
In past, I used something like: put the variable in one array and read the variable from that array, but I think this is possible, what I'm doing wrong?
Thanks in Advance
Tiago
I don't think you need the back-ticks on the last line. You're not trying to run the output of the serverStatus.sh script as a command itself, are you?

Any unix command to get the result of most-recently executed command?

For example,
I executed "pwd" and it shows the current working directory. Then if I want to reuse that result in my another command, it would convenient to get it via a Unix command or built-in variable. Does it exist?
You can get the result, as in return code, using $?. In order to get the output you'll need to explicitly keep it around - e.g. with:
MYVAR=`pwd`
echo $MYVAR
Use $? inorder to get the status of the last executed command. Its value will be zero if the last executed command was successful else non zero.
The internal variable $? holds the return value of the last executed command or program. Example: http://tldp.org/LDP/abs/html/complexfunct.html#MAX.
If you don't need to run one command first, you can also try using pipes | to connect commands. I am constantly piping long directory listings over to more, so I can page through the results, with
ls -al | more
so if you want to use the results of running pwd as input to another program, you can try something like piping the results of pwd over to more with
pwd|more

Resources