# Shell Scripts

**URL:** <https://community.unix.com/t/shell-scripts/171905>\
**Category:** AIX\
**Created:** [June 10, 2007, 11:41pm UTC](https://community.unix.com/t/shell-scripts/171905 "2007-06-10T23:41:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kwliew999](https://community.unix.com/letter_avatar/kwliew999/32/6_449c533f586939b6e5f246632a46421f.png) [@kwliew999](https://community.unix.com/u/kwliew999)\
**Post date:** [June 10, 2007, 11:41pm UTC](https://community.unix.com/t/shell-scripts/171905/1 "2007-06-10T23:41:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![suparnbector](https://community.unix.com/letter_avatar/suparnbector/32/6_449c533f586939b6e5f246632a46421f.png) [@suparnbector](https://community.unix.com/u/suparnbector)\
**Post date:** [June 10, 2007, 11:53pm UTC](https://community.unix.com/t/shell-scripts/171905/2 "2007-06-10T23:53:03Z")

</div>

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.](http://www.unix.com/showthread.php?t=13785)

---

<div class="post-metadata">

**Author:** ![kapilraj](https://community.unix.com/letter_avatar/kapilraj/32/6_449c533f586939b6e5f246632a46421f.png) [@kapilraj](https://community.unix.com/u/kapilraj)\
**Post date:** [June 11, 2007, 5:40pm UTC](https://community.unix.com/t/shell-scripts/171905/3 "2007-06-11T17:40:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kwliew999](https://community.unix.com/letter_avatar/kwliew999/32/6_449c533f586939b6e5f246632a46421f.png) [@kwliew999](https://community.unix.com/u/kwliew999)\
**Post date:** [June 11, 2007, 9:35pm UTC](https://community.unix.com/t/shell-scripts/171905/4 "2007-06-11T21:35:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![porter](https://community.unix.com/letter_avatar/porter/32/6_449c533f586939b6e5f246632a46421f.png) [@porter](https://community.unix.com/u/porter)\
**Post date:** [June 11, 2007, 9:41pm UTC](https://community.unix.com/t/shell-scripts/171905/5 "2007-06-11T21:41:21Z")

</div>

> [@kwliew999](#):
>
> How to check whether a particular file system have enough disk space for some process.

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.

---

<div class="post-metadata">

**Author:** ![kwliew999](https://community.unix.com/letter_avatar/kwliew999/32/6_449c533f586939b6e5f246632a46421f.png) [@kwliew999](https://community.unix.com/u/kwliew999)\
**Post date:** [June 12, 2007, 3:01am UTC](https://community.unix.com/t/shell-scripts/171905/6 "2007-06-12T03:01:04Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![solfreak](https://community.unix.com/user_avatar/community.unix.com/solfreak/32/553_2.png) [@solfreak](https://community.unix.com/u/solfreak)\
**Post date:** [June 12, 2007, 10:41am UTC](https://community.unix.com/t/shell-scripts/171905/7 "2007-06-12T10:41:10Z")

</div>

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

```nohighlight
#!/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

---

<div class="post-metadata">

**Author:** ![kwliew999](https://community.unix.com/letter_avatar/kwliew999/32/6_449c533f586939b6e5f246632a46421f.png) [@kwliew999](https://community.unix.com/u/kwliew999)\
**Post date:** [June 12, 2007, 8:06pm UTC](https://community.unix.com/t/shell-scripts/171905/8 "2007-06-12T20:06:42Z")

</div>

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