help - scripting command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help - scripting command
# 8  
Old 11-29-2011
well im doing something similar just checking if only a number is entered no letters can be entered. If a number is entered it should continue to the next part of the bash script. If a letter is entered, user has to try again until a number is entered

i think its an until loop but i cant get it to work


Code:
read -p "How many tags to enter:" cars
        echo $cars | grep "[a-zA-Z]"
        if [ $? -eq 1 ]
        then
        read -p "Please enter car year:" yearmodel
        else
        read -p"Invalid number. Please re-enter:" $cars
        exit

this is the next part of the script
Code:
  read -p "Please enter car year:" yearmodel

im using an until loop but it wont stop looping
# 9  
Old 11-29-2011
Try using "break" to come out of the loop once work is done.
# 10  
Old 11-29-2011
@gangsta
Please let us know exactly what Operating System and version and Shell you are running. The "read -p" is not "read with a prompt string" in POSIX Shell.
Code:
uname -a
echo $SHELL

It would really help if you replied to questions posed on your own threads.
Repeatedly posting the same code segment from a much larger script is not good.
Have you read cfajohnson's post?

Last edited by methyl; 11-29-2011 at 09:38 PM.. Reason: ambiguous
# 11  
Old 11-29-2011
this is what i have

Code:
      
read -p "How many cars to enter:" cars
until "$cars" [ -ne 1 ] && grep "[a-zA-Z]"
do
read -p "Invalid number. re-enter:" cars
done

# 12  
Old 11-29-2011
How about this example in bash :

Code:
while true
do
   read -p "How many cars to enter: " tot
   [ $tot -ge 0 ] 2>/dev/null && break
   echo "Invalid number. re-enter"
done
num=0
while [ $num -lt $tot ]
do
    read -p "Enter car #$((++num)): " cars[num]
done
num=0
while [ $num -lt $tot ]
do
   echo "Car #$((++num)) is ${cars[num]}"
done

or, using for (( )) loops:
Code:
while true
do
   read -p "How many cars to enter: " tot
   [ $tot -ge 0 ] 2>/dev/null && break
   echo "Invalid number. re-enter"
done
for((num=1; num<=tot; num++)) {
    read -p "Enter car #$num: " cars[num]
}
for((num=1; num<=tot; num++)) {
   echo "Car #$num is ${cars[num]}"
}

This User Gave Thanks to Chubler_XL For This Post:
# 13  
Old 11-30-2011
how do i make the error message only to be displayed if a wrong answer is diplayed?

Code:
while true
do
   read -p "How many cars to enter: " tot
   [ $tot -ge 0 ] 2>/dev/null && break
   echo "Invalid number. re-enter"
done

# 14  
Old 11-30-2011
That's what it does for me now:

Code:
$ cat gangsta
#!/bin/bash
while true
do
   read -p "How many cars to enter: " tot
   [ $tot -ge 0 ] 2>/dev/null && break
   echo "Invalid number. re-enter"
done
$ ./gangsta
How many cars to enter: two
Invalid number. re-enter
How many cars to enter: -1
Invalid number. re-enter
How many cars to enter: 3
$./gangsta
How many cars to enter: 5
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Escape and command substitution in scripting

I have one question in shell script for escape "\" with command substitution "` `". I post there to seek help to understand how it works. My original one piece of code in script like this: This piece of code in whole script is working without errors chk_mode=`sqlplus -s /nolog<<EOF connect /... (4 Replies)
Discussion started by: duke0001
4 Replies

2. Shell Programming and Scripting

Scripting with arguments for ping command

This is the script I already have but I have problems with two arguments the first argument -t , I want to count 200 by the last digit of the IP address for example when I run the script ./ping.sh -t 17, the output would be192.168.0.217 is upThe second arguments --up won't work. Could anybody... (1 Reply)
Discussion started by: Roggy
1 Replies

3. Shell Programming and Scripting

Which is the best way/command to do mathematics in UNIX scripting?

Hello all! I used to use expr for doing simple mathematics, but has a main advantage and a main disadvantage: The advantage is that it can take variables for numbers (e.g.{1}: echo "Give me first" read lol echo "Give other" read lil sum=`expr $lol + $lil` echo "The sum of $lol and $lil =... (5 Replies)
Discussion started by: hakermania
5 Replies

4. UNIX for Dummies Questions & Answers

Linux scripting problem using ps command

when I try to apply this principle to my script, is not working, it tells me something like 'command not found' what i wrote is this.. if ; then echo "message1"; else echo "message2"; fi I'm supposed to count number of processes of the user, and if they're five or more, then a message... (2 Replies)
Discussion started by: aric87
2 Replies

5. Shell Programming and Scripting

shell scripting best practices - trap command

I know there is a command called trap which can be used to capture the signals from a shell script and redirect the control to a required function (such as a cleanup). My question is - Those of you who have written lot of shell scripts - do you always write a set of trap commands to capture... (4 Replies)
Discussion started by: sagar_evc
4 Replies

6. UNIX for Dummies Questions & Answers

Trying a arithmatic on a command line vs scripting.

Hi Everyone! I'm trying to do simple math on a single command line instead of a script which I've already set up using let etc. I can not get the same output to display on a command line. Essentially I would like a=20, b=50, and c=a*b. When I tried: let "A=20, B=50"; let C=A*B; echo $C ... (2 Replies)
Discussion started by: CasperQuiet
2 Replies

7. Shell Programming and Scripting

Help required on scripting the rm -f command

When i execute rm -f $1 via a script file named rmf, it is not deleting all the files, say starting with "sec". i have execute rmf for many times to remove all the occurrences... $rmf sec* - this should delete all files starting with sec, but not. The rm -f sec* is working fine. kindly help... (3 Replies)
Discussion started by: frozensmilz
3 Replies

8. Shell Programming and Scripting

Scripting using the FIND command

I'm working on a 'find' command which uses the -exec option to tar a listing of files that it finds. Is there a single option available that would both 1) create the TAR file if it doesn't exist and/or 2) update the TAR file if it already exists? Currently when I want manually create a TAR... (3 Replies)
Discussion started by: buechler66
3 Replies

9. Shell Programming and Scripting

Need help with Scripting diff command

Here is where I am at so far..... ------------------------- #!/bin/bash echo "Enter the output file name" read output_file echo "Enter the Orginal file Name" read write_file d= ' diff $write_file $output_file ' if $d = 1 ;then echo "files are not identical" else echo "they... (2 Replies)
Discussion started by: tlfletcher05
2 Replies

10. UNIX for Dummies Questions & Answers

shell scripting my own diff command

Hi I would like to run the diff command and recieve a little different output. I am on a linux machine. I am pretty new to shell scripting. So far my idea has shaped up to this, unworking, script. I would like file1: and file2: instead of the usual > or < output you recieve, diff | sed -e ... (4 Replies)
Discussion started by: axcxe
4 Replies
Login or Register to Ask a Question