Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-02-2007, 02:31 PM
|
#1
|
Member
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324
Rep:
|
Shell script next/skip/do nothing
Good Day,
Hod do I tell shell script to go to next step if condition becomes true.
like
Code:
declare count[9]=( 2 3 4 5 6 7 7 5 2)
for (i=0;1<8;1++)
do
if $count[i] =2
next
else
echo $count[i];
done
Thanks.
|
|
|
11-02-2007, 02:51 PM
|
#2
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
By "go to the next step", do you mean break out of the loop? You already have an "if-else" construct (although "fi" is missing)
The "break" and "continue" commands might be what you are looking for.
A good tool is to use the Advanced Bash Scripting Guide (ABS) for searches (ABS is free at http://tldp.org---also look at Bash Guide for Beginners)
|
|
|
11-02-2007, 02:53 PM
|
#3
|
Member
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Rep:
|
Quote:
Originally Posted by knockout_artist
Good Day,
Hod do I tell shell script to go to next step if condition becomes true.
like
Code:
declare count[9]=( 2 3 4 5 6 7 7 5 2)
for (i=0;1<8;1++)
do
if $count[i] =2
next
else
echo $count[i];
done
Thanks.
|
You're looking for 'break' instead of 'next'. From the bash man page:
Code:
break [n]
Exit from within a for, while, until, or select loop. If n is
specified, break n levels. n must be >= 1. If n is greater than
the number of enclosing loops, all enclosing loops are exited.
The return value is 0 unless the shell is not executing a loop
when break is executed.
You also need to look at the syntax for 'if' and 'test' in the Bash man pages; I think that there are some issues in your code.
|
|
|
11-02-2007, 03:00 PM
|
#4
|
Member
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324
Original Poster
Rep:
|
Thank you both of you.
But
Code:
(2 4 5 6 7 9 7 6 2)
my actual code is
Code:
declare dir[8]=( 2 3 4 6 7 6 5 2)
echo " here are all the directories"
echo "-------------------------------"
for ((i=0;i<=7; i++))
do
if [ ${dir[0]} -eq 2 ] ; then
skip
else
# [ ${dir[0]} -ne 2 ] ; then
echo '${dir[0]}'
fi
done
I need to print every thing but 2.
If i am able to explain it better.
Last edited by knockout_artist; 11-02-2007 at 03:02 PM.
|
|
|
11-03-2007, 12:38 AM
|
#5
|
Member
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Rep:
|
Ah. You want 'continue'
This time I actually tested the code ;-)
Code:
declare dir[8]=( 2 3 4 6 7 6 5 2)
echo " here are all the directories"
echo " ----------------------------"
for ((i=0;i<=7; i+=1))
do
if [ ${dir[$i]} -eq 2 ] ; then
continue
else # [ ${dir[0]} -ne 2 ] ; then
echo "${dir[$i]}"
fi
done
by the way, the use of the array is interesting, but I think it's overkill. If I were writing this, it would have looked like this:
Code:
echo " here are all the directories"
echo " ----------------------------"
for dir in 2 3 4 6 7 6 5 2
do
if [ $dir -eq 2 ] ; then
continue
else
echo "$dir"
fi
done
|
|
|
11-03-2007, 01:44 PM
|
#6
|
Member
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324
Original Poster
Rep:
|
^ thank you very much.
I think you have solved the issue for me.
The 2 reasons I have used array, eventually this array will hold the names of directories.
Code:
if
read only
continue
else
chmod -R
fi
Plus most of the programming I have done is in C :-)
Last edited by knockout_artist; 11-03-2007 at 01:46 PM.
|
|
|
11-04-2007, 12:45 AM
|
#7
|
Member
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Rep:
|
Quote:
Originally Posted by knockout_artist
I think you have solved the issue for me.
The 2 reasons I have used array, eventually this array will hold the names of directories.
|
You still don't need an array... check this out:
Code:
listing="
./foo
./bar
./baz
./quux
./quuux
"
for dir in $listing
do
echo $dir
done
Have fun. :-)
|
|
|
All times are GMT -5. The time now is 05:33 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|