LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2013, 01:47 PM   #1
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Rep: Reputation: Disabled
script stops if unable to navigate to mentioned path


Hello Team

I have set of folder which i am taking as input and then navigating to one fixed path. There are few folders which dont have that path becuase of which the script is geting stopped at that point. Please suggest how to overcome this. I want to continue with next folder input if present one don't have defined path. Below is the script i am trying.

Below is the error i am getting when executing the script.

Quote:
test1.sh: /path/dataintigrity/cubes/: does not exist
Below is the code:

Code:
fix=1851162600
DIR=/path/
SCRIPT_DIR=/path/scripts/
cd $DIR

for folder in `ls -ltr |egrep '^d' |awk '{print $9}'`
do
	cd /path/$folder/cubes/
		if [ $? -eq 0 ]
		then
		echo `ls -ltr |awk '{print $5}'` >$SCRIPT_DIR/size.txt
		for size in `cat $SCRIPT_DIR/size.txt`
		do
			if [ $size -gt $fix ]
			then
		echo "Cube in folder $folder has crossed 1.7G, please take appropriate action" | mailx -s "cube file has exceeded 1.7G size" xyz@abc.com
			fi
		done
		fi
done
 
Old 03-12-2013, 02:04 PM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
for folder in `ls -ltr |egrep '^d' |awk '{print $9}'`
Before you go any further or write another line of shell script you must read this: http://partmaps.org/era/unix/award.html.

I'm serious.

Now that you've read that page, what you want is:
Code:
for dir in /path/*/cubes/; do
    cd "$dir" || continue
    ls -ltr | awk '{print $5}' | while read size; do
        if [ $size -gt $fix ]; then
            echo "Cube in folder $folder has crossed 1.7G, please take appropriate action" | 
                mailx -s "cube file has exceeded 1.7G size" xyz@abc.com
        fi
    done
done
By the way, you were getting the massage because -x option was set, probably via set -x somewhere in your script or #!/bin/sh -x at the beginning.

PS. Did you know, that you can use find to do exactly what you did in this script? Go read man find. The option you will be most interested in is -size.

Last edited by mina86; 03-12-2013 at 02:05 PM.
 
1 members found this post helpful.
Old 03-12-2013, 02:20 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
1) Don't Read Lines With For

2) Don't parse ls for filenames and metadata.

3) QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

4) $(..) is highly recommended over `..`

5) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

6) Useless Use Of Grep.

7) Since environment variables are generally all upper-case, it's recommended practice to keep your own user variables in lower-case or mixed-case to help differentiate them. You should also generally try to use variable names that clearly relate to their function in the script, but avoid ones that conflict with existing command names like "file".

8) Scripting With Style



Congratulations! I think this one post has made me post more of my canned responses at once than any other.

Start by correcting the worst of the above, particularly the first three, then come back if you still have problems.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to navigate to folder on server via browser SupremeUK Linux - Server 1 09-30-2011 05:29 AM
LD_LIBRARY_PATH is mentioned with the library path: /usr/bin/ld: cannot find -ljson ven_visu Linux - Software 1 08-05-2011 09:27 AM
how to navigate to subdirectories in a shell script jamesbon Programming 6 11-24-2010 09:33 AM
copy the file whose source path is mentioned in a file to the specified Destination bluepenguine Linux - General 1 06-03-2010 05:31 AM
Unable to navigate the web. garthron Linux - Networking 1 05-08-2003 03:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration