Solved

Bash script, string validation help

Posted on 2007-04-04
4
497 Views
Last Modified: 2013-12-26
hello
i have a small code
lets say:
echo "Give me the port"
read port
how can i see if $port is formed only by numbers and do a while (while port is not formed only by numbers echo "the port is wrong written, type it again")

thanx
0
Comment
Question by:rares_dumitrescu
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 18848993
while echo $port | grep -v '^[0-9]*$' ; do
  echo "Give me the port"
  read port
done
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 150 total points
ID: 18848998
#!/bin/bash
while true
do
  echo -e "Give me the port: \c"
  read port

  [ -z "$port" ] && continue
  echo $port | grep -q "^[0-9][0-9]*$" && break
  echo "the port is wrong.  Please type it again"
done

echo "Port number is $port"
0
 

Author Comment

by:rares_dumitrescu
ID: 18849324
Any ideeas on how to put this in a while for a dialog menu ?

The code is:
port=1501

if [ "$answer" = "1" ]; then
cat /dev/null > $tempfile
   dialog --title "You are using the command - getpsy" \
          --inputbox "Please select the port for the psybnc \n\
You are now using the port: $port" 8 45 $port
   port=`cat $tempfile`

while [ -here if the port is lower than 1500 or it contains charcters, the script should ask for the port again- ]
do
cat /dev/null > $tempfile

   dialog --title "You are using the command - getpsy" \
          --inputbox "Please select the port for the psybnc \n\
You are now using the port: $port" 8 45 $port
   port=`cat $tempfile`
done
cat /dev/null > $tempfile
fi

What can i put in that while [ ] ?
Thanx
0
 
LVL 84

Expert Comment

by:ozo
ID: 18849439
while echo $port | grep  '[^0-9]' || test "$port" -lt 1500 ; do
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 49
ACL in Solaris 10 & AIX V6.1 to circumvent application required  grp:rwx 9 114
if loop in java 3 139
Problem to start Neon 20 104
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question