- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- basic shell scripting .. help needed
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2010 03:56 PM
тАО06-02-2010 03:56 PM
basic shell scripting .. help needed
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 ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2010 07:24 PM
тАО06-02-2010 07:24 PM
Re: basic shell scripting .. help needed
#!/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+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2010 11:24 PM
тАО06-02-2010 11:24 PM
Re: basic shell scripting .. help needed
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2010 03:20 AM
тАО06-03-2010 03:20 AM
Re: basic shell scripting .. help needed
if [ -d /home/$1 ]
-d means in this case...
thanks again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2010 04:24 AM
тАО06-03-2010 04:24 AM
Re: basic shell scripting .. help needed
-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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2010 04:26 AM
тАО06-03-2010 04:26 AM
Re: basic shell scripting .. help needed
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2010 04:32 PM
тАО06-03-2010 04:32 PM
Re: basic shell scripting .. help needed
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2010 06:43 PM
тАО06-03-2010 06:43 PM
Re: basic shell scripting .. help needed
>>-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+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2010 12:25 AM
тАО06-04-2010 12:25 AM
Re: basic shell scripting .. help needed
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