LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-26-2007, 05:32 PM   #1
christianunix
Member
 
Registered: Oct 2007
Posts: 40

Rep: Reputation: 15
Trying to write a bash script


There is a while loop in the bash script,

and in the while loop the script takes three inputs from the user,

and then when user types letter x then the loop stos.

I have the following so far:

Code:
#!/bin/bash

while echo "first input>"

read firstinput
[ $firstinput != 'x' ]

do 

echo "second input?"

read secondinput

if [ $secondinput = 'x' ]; then

"$firstinput"="x"

fi

echo "third input?"

read thirdinput

if [ $thirdinput = 'x' ]; then

"$thirdinput"="x"

fi

done
but the above isn't working.... I guess I will try the break command

Last edited by christianunix; 10-26-2007 at 05:45 PM.
 
Old 10-26-2007, 06:00 PM   #2
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Try this:

Code:
while true
do
    echo "$PS3"

    stuff

    if [ "$input" = "x" ]
    then
        break
    fi
    # or more compactly:

    [ "$input" = "x" ] && break
done
 
Old 10-26-2007, 06:08 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
while echo "first input>"
read firstinput
[ $firstinput != 'x' ]

do
The general form of while is:
while <expr>
do
.
.
done

In your case, <expr> is everything in bold, which does not look like it's going to work.

Also, there's nothing to break you out of the loop based on the second and third entries

I would do it like this (pseudo-code---not checked--maybe not even correct BASH...):

count =1
while [ $count -lt 4 ]
do
echo "enter data: "
if [ $data = "x" ]; then break
else
entry[count] = $data
count += 1
done


The idea is to read into the same variable each time, so you only have one test for "x". When it sees no "x", then it stuffs the data into an array.
 
Old 10-26-2007, 06:09 PM   #4
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Supplement: man bash tells me that there is also an "until" command, so this would also work and be slightly cleaner:

Code:
NAM=""
until [ "$NAM" = "x" ]
do
    read NAM
    echo "NAM = $NAM"
done
Sometimes it is also a good idea to do the text comparison like this: [ "w$NAM" = "wx" ] because if the variable $NAM is a valid flag for inside a test, e.g. if it's -f, the code might break otherwise. You will have to test to see whether that is a problem for you.

Have fun!
 
Old 10-26-2007, 06:22 PM   #5
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Nice idea to use arrays! Alas I often am forced against my will to use a shell that doesn't support them. It's called ksh. (deep unhappiness)

If you are going to use a loop then maybe this is an opportunityy for a for ((integer code)) expression. This works and includes pixellany's idea:

Code:
for ((count=1; count <= 3 ; count++ ))
do
    echo "enter data: "
    read data
    [ "$data" = "x" ] && break
    entry[$count]=$data
    echo "Have set array entry $count to ${entry[$count]}"
done

Last edited by Tischbein; 10-26-2007 at 06:22 PM. Reason: Removed trailing garbage.
 
Old 10-26-2007, 06:30 PM   #6
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 14
Posts: 540

Rep: Reputation: 32
Try this

#!/bin/sh
while [ $1 != x ]; do
# do stuff with $1 ie
echo $1 # or cp $1 /path/to/my/dir or mpg321 $1
shift
done

you just have to pass the script as many parameters as you need adding x for terminating.

usage:

./myscript song_1.mp3 song_2.mp3 song_3.mp3 x

The script will play song_1/2/3 and then stop



This also could be fine
until [-z $1 ]; do
echo $1 # same as before
shift
done

This don't even need a terminating parameter

Ciao

Last edited by urka58; 10-27-2007 at 05:08 AM.
 
  


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
read write in bash script yhus Programming 6 09-18-2006 01:23 PM
Trying to write a bash script -- :Command not found? bpottle Linux - Software 7 06-10-2006 07:11 PM
Need to write a bash script imagineers7 Linux - General 5 05-10-2006 12:17 AM
Bash: how to write a wrapper script? J_Szucs Programming 0 01-29-2005 06:50 PM
Anoyone willing to write a BASH script for me? pilot1 Programming 4 09-16-2003 09:56 PM


All times are GMT -5. The time now is 12:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration