LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script next/skip/do nothing (http://www.linuxquestions.org/questions/linux-newbie-8/shell-script-next-skip-do-nothing-596682/)

knockout_artist 11-02-2007 03:31 PM

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.

pixellany 11-02-2007 03:51 PM

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)

bartonski 11-02-2007 03:53 PM

Quote:

Originally Posted by knockout_artist (Post 2946090)
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.

knockout_artist 11-02-2007 04:00 PM

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.

bartonski 11-03-2007 01:38 AM

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


knockout_artist 11-03-2007 02:44 PM

^ 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 :-)

bartonski 11-04-2007 01:45 AM

Quote:

Originally Posted by knockout_artist (Post 2947051)
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 10:14 PM.