LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 11-02-2007, 02:31 PM   #1
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
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.
 
Old 11-02-2007, 02:51 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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)
 
Old 11-02-2007, 02:53 PM   #3
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by knockout_artist View Post
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.
 
Old 11-02-2007, 03:00 PM   #4
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
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.
 
Old 11-03-2007, 12:38 AM   #5
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
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
 
Old 11-03-2007, 01:44 PM   #6
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
^ 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.
 
Old 11-04-2007, 12:45 AM   #7
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by knockout_artist View Post
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. :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
Call One shell script from another shell script Sundaram Linux - Software 5 10-13-2006 03:59 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration