System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

basic shell scripting .. help needed

iinfi1
Super Advisor

basic shell scripting .. help needed

well i v jus started to learn scripting through some videos i have. is tldp.org/LDP/abs/html a good site to learn scripting? any other better place??

i was following a script to check the space occupied by each user in their home directory.
I have users u1 u2 u3
[code]
linux-lw75:~/scripts # cat spaceloop.sh
#!/bin/bash
#This script determines the size of a user's home directory

if [ $# -lt 1 ]
then
echo "you must pass atleast one argument to the $0 script"
exit
fi

while [ $# -ge 1 ]
do
cd /home/$1
space=`du -s | cut -f 1`
echo "$1 is using $space kilobytes"
shift
done

linux-lw75:~/scripts # sh spaceloop.sh u1 u2[color=red] u10 [/color]u3
u1 is using 1680 kilobytes
u2 is using 100 kilobytes
spaceloop.sh: line 12: cd: /home/u10: No such file or directory
u10 is using 100 kilobytes
u3 is using 100 kilobytes
linux-lw75:~/scripts #

[/code]

the above script runs fine when the parameters provided are correct ... eg in the above case when i provided 'u10' (which is not present in the system), the script could not handle it. What statement included above "space=`du -s | cut -f 1`" can catch the error "spaceloop.sh: line 12: cd: /home/u10: No such file or directory"

thanks ...
8 REPLIES
Kapil Jha
Honored Contributor

Re: basic shell scripting .. help needed

Hi You need to put one more line in ur script to catch the error.


#!/bin/bash
#This script determines the size of a user's home directory

if [ $# -lt 1 ]
then
echo "you must pass atleast one argument to the $0 script"
exit
fi

while [ $# -ge 1 ]
do
if [ -d /home/$1 ]
cd /home/$1 2>/dev/null
then
space=`du -s | cut -f 1`
echo "$1 is using $space kilobytes"
fi
shift
done

, there are so many things you can do.
Best thing is to run scripts as

spaceloop.sh -x u1 u2 u10
and it would tell you what exactly its happening and would help u in getting on the error.

BR,
Kapil+
I am in this small bowl, I wane see the real world......
Prasanth V Aravind
Trusted Contributor

Re: basic shell scripting .. help needed

one trick to script troubleshooting.


Any error running script ??? go for debug option

Modify shell definition to "#!/bin/bash -x" in script

it will give clear out with commands executing & output of each commands... its a very easy method, you can troubleshoot any script with your own.

Gudluck
Prasanth
iinfi1
Super Advisor

Re: basic shell scripting .. help needed

thank you .. there is a long way to go before i understand scripting well.. for a system admin i should know perl n shell well .. like in this case i dont understand even what
if [ -d /home/$1 ]
-d means in this case...

thanks again
Michal Kapalka (mikap)
Honored Contributor

Re: basic shell scripting .. help needed

hi,

-d => To check if a directory exists

example :


To check if a directory exists in a bash shell script you can use the following:

if [ -d "$DIRECTORY" ]; then
# Control will enter here if $DIRECTORY exists
fi
Or to check if a directory doesn't exist:

if [ ! -d "$DIRECTORY" ]; then
# Control will enter here if $DIRECTORY doesn't exist
fi

mikap
Michal Kapalka (mikap)
Honored Contributor

Re: basic shell scripting .. help needed

hi,

you oculd check this how-to as an example :

"Linux/UNIX: Find Out If File Exists With Conditional Expressions"

http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html

mikap

macosta
Trusted Contributor

Re: basic shell scripting .. help needed

The fun thing about shell scripts, like any program, is error handling. If you can 100% guarantee the input, it's not a big deal, but that's normally not the case.

Read the "bash" manpage, and feel free to post any questions here. If you want to find out the test flags, see the section for "test."

Here's a short script that works on Cygwin to enumerate active users' home directories:
--
for f in $(cut -d: -f6 /etc/passwd); do
# If it's a directory
[[ -d "$f" ]] &&
# Change over to that directory (in case it's a symlink)
{ cd "$f";
# disk usage on current directory
du -sh .;
# Change back - probably not necessary, but why not?
cd -; };
done
--

If you want to deal with input, check out the "getopts" shell builtin that handles arguments. They're made so you don't have to re-implement a common need.
Kapil Jha
Honored Contributor

Re: basic shell scripting .. help needed

>>f [ -d /home/$1 ]
>>-d means in this case...

iinfi, I think we can point out whats wrong where int he forum, we can not teach shell/perl script in the forum.

Happy Learning!!

BR,
Kapil+
I am in this small bowl, I wane see the real world......
iinfi1
Super Advisor

Re: basic shell scripting .. help needed

hi all

thanks a lot for your help ... yea i do understand forum is a place to clear doubts and not learn from scratch ...
i am learning scripting now.. will post here if i have further doubts
thanks