Shell Scripts

I would like to seek some expertise of all our AIX experts on board.

1) I would like know how to get a return exit code of a command. I found that there are exist code for each and every command run in AIX but I just can't get the return code from my scripts.

A=`cp /home/abc/abc.txt /home/cde`
echo $A

If the return code exist, it should return to A and display on screen with the above scripts, but it doesnt. Anyone can help?

2) How to sucstract 2 months from current system month using AIX script?

date +"%h%Y" can show me Jun2007, I would like to get a Apr2007 result, how am I going to write the script?

Thanks in advance.

1 I dont have AIX but on HP i use echo $? to see the status of last command.
0 means success
>0 means failure

2 Subtract Date: Hope you can find some help from this link.
Date Calc.

By doing "A=`cp /home/abc/abc.txt /home/cde`" , you are trying to store the STDOUT of the command to a variable called "A".

The return code is stored on a variable called "?"

So infact you should do ,

cp /home/abc/abc.txt /home/cde
echo $? # Anything other than "0" is a failure normally unless you programmed it differently.

Regards,

Kaps

Thank you suparnbector and kapilraj for your kindness help. I have solved my problem on the shell scripts.
But I have another question on the shell scripts as well.
3) How to check whether a particular file system have enough disk space for some process. I would like to write a script to check whether file system /abc has 5GB space for some other processes. Is there any commands that I can use directly to check or I need to write a script?

Tq.

The normal approach is to use "df" and parse the results. Giving a path will just return you stats on that path, and the "-k" option gives you results in kilobytes rather than 512byte sectors.

eg "df -k $HOME" or "df -k ." etc.

I understand the usage of df -k. The script I am looking for is to capture the free space from df -k for particular file system and compare with the minimum space to return a result of whether it is "Adequate disk space" or "Lack of disk space"

Depending on how df print's the output on your system you might need to modify this:

#!/bin/sh

if [ `df -k $1 | awk '{getline; print $4}'` -ge `expr 5 \* 1024 \* 1024` ] ; then
    echo "Adequate disk space"
else
    echo "Lack of disk space"
fi

Invoke the script as: script filesystem

solfreak, thanks a lot for your script. It helps. The only changes I made is the position changed to $3 instead of $4.