help - scripting command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help - scripting command
# 15  
Old 11-30-2011
@gangsta: What's the purpose of "2>/dev/null" in "[ $tot -ge 0 ] 2>/dev/null && break"? What do you think STDERR would contain upon this conditional check that you want to direct it to /dev/null?
# 16  
Old 11-30-2011
Quote:
Originally Posted by Chubler_XL
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
$

yea same thing for me but i only want it to show "How many cars to enter" the one time after that if a wrong answer is entered it should only be "Invalid number. Re-enter"
# 17  
Old 11-30-2011
Quote:
Originally Posted by balajesuri
What's the purpose of "2>/dev/null" in "[ $tot -ge 0 ] 2>/dev/null && break"? What do you think STDERR would contain upon this conditional check that you want to direct it to /dev/null?
It will contain things like:
Code:
Input: two
line 5: [: two: integer expression expected
Input:
line 5: [: -ge: unary operator expected

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

This User Gave Thanks to Chubler_XL For This Post:
# 18  
Old 11-30-2011
Code:
#!/bin/bash
is_int()
{
  case $1 in
    *[!0-9]*|"") return 1 ;;
  esac
}

while :
do
  read -ep "How many cars to enter: " tot
  is_int "$tot" && [ "$tot" -gt 0 ] && break
  printf "%s\n"  "Invalid number: $tot"\
                 "Please enter a number greater than 0"\
                 "Press ENTER to continue"
  read
done

This User Gave Thanks to cfajohnson For This Post:
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