Results 1 to 2 of 2
Thread: simple script problem
Enjoy an ad free experience by logging in. Not a member yet? Register.
- Join Date
- Apr 2013
simple script problem
I need to write a shell script that will prompt the user for a single integer and display twice its value. Then do a preliminary evaluation of the user's input and repeat its numeric value before displaying the result of the operation. (I.e., check for non-numeric or non-integer input and inform the user)
what I have so far is this:
#!/bin/sh echo "enter an integer" read integer integer=(($integer*2)) if [ $integer= ] ; then echo $integer else echo "error your input is not an integer"
Last edited by atreyu; 04-18-2013 at 07:56 PM. Reason: added CODE tags to aid in readability
- Join Date
- May 2011
there are a million ways to check for a number. i like to use grep.
also, i usually put the "read" part in a loop, to make sure something is entered. if i want it to match on something, i put that in the loop, too. here's an example:
#!/bin/bash until [ -n "$input" ]; do echo -n "enter an integer: " read input if [ -n "$input" ]; then echo $input|grep ^[0-9][0-9]*$ if [ $? -ne 0 ]; then echo "$input: input is not a number" unset input fi fi done integer=$(( $input * 2 )) echo Input integer is $input echo Integer doubled is $integer