how to display an error if input value is not an IP address??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to display an error if input value is not an IP address??
# 1  
Old 03-21-2010
Question how to display an error if input value is not an IP address??

Dears,

I need your assistance, I'm creating a shell script to read the IP address of a server and i would like to display an error message in red if the entered value is not an IP. any ideas how can i do it?Smilie

#!/bin/sh

echo "Please enter the server IP address: \c"
read IPadd
????????????????????????????
????????????????

Similarly if i have the date in format yyyymmdd and i need to convert it to yymmdd, how can i do it? and how to display error message if the entered date is not in the correct (requested) format yyyymmdd??

Thanks in advance
# 2  
Old 03-21-2010
if ipv4, it does 3 points (.) for define an IP...

Code:
var=$(echo $IPadd | awk -F"." '{print $4}')
if [ -z $var ];then echo "Enter a correct format";fi

# 3  
Old 03-21-2010
Quote:
Originally Posted by Dendany83
Dears,

I need your assistance, I'm creating a shell script to read the IP address of a server and i would like to display an error message in red if the entered value is not an IP. any ideas how can i do it?Smilie

Please put code inside [code] tags.
Quote:
Code:
#!/bin/sh

echo "Please enter the server IP address: \c"
read IPadd
????????????????????????????
????????????????


Code:
is_validip()
{
	case "$*" in
	""|*[!0-9.]*|*[!0-9]) return 1 ;;
	esac

	local IFS=.  ## Bash only
	set -- $*

        [ $# -eq 4 ] &&
	    [ ${1:-666} -le 255 ] && [ ${2:-666} -le 255 ] &&
	    [ ${3:-666} -le 255 ] && [ ${4:-666} -le 254 ]
}

printf "%s: " "Please enter the server IP address"
read IPadd

if ! is_validip "$IPadd"
then
  printf "\e[31m%s\e[0m\n" "$IPadd"  ## Assumes a standard terminal
fi

Quote:

Similarly if i have the date in format yyyymmdd and i need to convert it to yymmdd, how can i do it? and how to display error message if the entered date is not in the correct (requested) format yyyymmdd??

Code:
printf "%s: " "Enter date in YYYYMMDD format"
read date

case $date in
  [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) ;;
  *) printf "%s\n" "Invalid date"; exit 1 ;;
esac

shortdate=${date#??}

You might also want to check that the values are in the correct range.
# 4  
Old 03-21-2010
if you have GNU date you can simply check the retrun code :
Code:
read -p "Enter date (YYYYMMDD) " DTE
if DTE=$(date -d "$DTE" +%y%m%d)
then echo "date is $DTE"
else echo "$DTE is an invalid date" # but date command will write its error on output
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script that takes IP address as an Input and deletes 7 lines

I have a flat file that contains a list of IP address following 6 additional lines. I would like to get a help in the following. a shell script that would take ip address or list of ip addresses as input, search one by one the ip address in the file and as soon as it find the exact match it... (12 Replies)
Discussion started by: knijjar
12 Replies

2. Programming

Getting error address already in use in socket programming

Hi , I am getting error in bind function of "Address already in use" even also i have used the setsockopt function in that case . Please help int ret = 0; int listenSock = 0; struct sockaddr_in myAddr; struct sockaddr_in ... (1 Reply)
Discussion started by: vipin auja
1 Replies

3. Shell Programming and Scripting

display Range of date depend on user input php

Hi, i am very new to php Is it possible to display Range of date depend on user input day example: user input 2 day start from 28/4/12 it will add 2 day from date of input so display should look like this 28/4/12 to 30/4/12 then from 30/412 user add another 4 date so will... (0 Replies)
Discussion started by: guidely
0 Replies

4. Solaris

CJS installation problem(client display:requesting internet address for 0:21:28:17:80:f4)

hi can anyone fine a solution thwe the problem that am facing i was trying to install sun4v client vth sol10_U8, but when i played "boot net" its asking for "requesting internet address for 0:21:28:17:80:f4 plz give me a sol for this (1 Reply)
Discussion started by: all_is_well
1 Replies

5. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies

6. Solaris

Error: Memory Address Not aligned

Hi, The following error message occured when I was trying to reboot my SUN machine: Memory address not aligned Its a Sun 280 R , Ultra SPARC III What should I do. Varma (3 Replies)
Discussion started by: gunnervarma
3 Replies

7. Shell Programming and Scripting

Shell script to parse/split input string and display the tokens

Hi, How do I parse/split lines (strings) read from a file and display the individual tokens in a shell script? Given that the length of individual lines is not constant and number of tokens in each line is also not constant. The input file could be as below: ... (3 Replies)
Discussion started by: yajaykumar
3 Replies

8. Programming

Incorrect address display by GDB

Hi, On using GDB debugger on different executables, the address displayed for the symbols seem incorrect, as it shows me the same address in each prorgram, run simultaneoulsy. eg: Program: linkmain1.c Breakpoint 1, main () at linkmain1.c:14 14 printf("In linkmain1.c\n"); (gdb)... (0 Replies)
Discussion started by: karthikb23
0 Replies

9. Shell Programming and Scripting

display result from user input

how can i only display the result according to user input? For example: i have a data file(in the attachement), when the user enter "1" it will only display whole records with pid from the data file matches to the users entered. (13 Replies)
Discussion started by: thms_sum
13 Replies

10. Shell Programming and Scripting

replace <Address> with a var input by user

Hi, All here is not a full version script, just post a sample. #/bin/bash read -p 'Addr: ' addr sed 's/<Address>/'"$addr"'/' pf.mod>$1 If the input string include `/', sed will return error message. I know that s/// can be replaced with s::: or s!!!, etc, but the input var can accept... (4 Replies)
Discussion started by: r2007
4 Replies
Login or Register to Ask a Question