Bash script, string validation help

Posted on 2007-04-04
Last Modified: 2013-12-26
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")

Question by:rares_dumitrescu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 84

Accepted Solution

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

Assisted Solution

Tintin earned 150 total points
ID: 18848998
while true
  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"

echo "Port number is $port"

Author Comment

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

The code is:

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- ]
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`
cat /dev/null > $tempfile

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

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Percona cluster member randomly crash 15 544
sumNumber challenge 16 140
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 468
mapAB Challlenge 35 173
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…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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