How to check the user input to be valid using shell script? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to check the user input to be valid using shell script?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-01-2013
hyeewang hyeewang is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 2 March 2013, 9:28 AM EST
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script?

The valid input is in the format like as follows.
1. It can only have r,w,x or a hyphen and nothing else.
2. ensure the r, w, x are in the correct order.

for example: rwxr-xr-x is a valid format.

Thanks
Sponsored Links
    #2  
Old 03-01-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 September 2014, 1:14 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,251
Thanks: 45
Thanked 817 Times in 644 Posts
First, your input (obviously a filemode mask) has more structure: the "r" can only appear in the 1nd, 4th or 7th place, the "w" can only appear in the 2rd, 5th or 8th place, etc.

Construct a regular expression for this and compare the input string against this regexp:


Code:
typeset input=""

print - "Enter input string: " ; read input
if print - "$input" | grep -q '^[-r][-w][-x][-r][-w][-x][-r][-w][-x]$' ; then
     print - "input: $input \tstatus: OK"
else
     print - "input: $input \tstatus: Not OK"
fi

I hope this helps.

bakunin
The Following User Says Thank You to bakunin For This Useful Post:
hyeewang (03-02-2013)
Sponsored Links
    #3  
Old 03-02-2013
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
 
Join Date: Nov 2007
Last Activity: 26 August 2014, 4:33 PM EDT
Location: New Zealand
Posts: 1,041
Thanks: 20
Thanked 25 Times in 24 Posts
If we're being picky (and I do love to be picky), you should add the sticky, setuid, and setgid flags too.
Resulting in:
^[-r][-w][-xXs][-r][-w][-xXs][-r][-w][-xXt]$
(The capital letter X is where you've got a forth byte set but no execute).
The Following User Says Thank You to Smiling Dragon For This Useful Post:
hyeewang (03-02-2013)
    #4  
Old 03-02-2013
hyeewang hyeewang is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 2 March 2013, 9:28 AM EST
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Thank you 2.
I got your idea although I can not run your code due to compile error.
Is there any convenient command to convert or substitute "rwxr-xr-x" to be "111101101"?
That means "change "r","w","x" to be "1", "-" to be "0".
After that, I want to change binary to octal format by "bc"? Is it a good method by "bc"?
I am a beginner. thanks for help.
Sponsored Links
    #5  
Old 03-02-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 September 2014, 1:14 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,251
Thanks: 45
Thanked 817 Times in 644 Posts
Quote:
Originally Posted by hyeewang View Post
I got your idea although I can not run your code due to compile error.
What do you mean by "compile error"? What i wrote was Korn-Shell code. Which programming language are you using? (If not a shell, you are probably in the wrong forum. Say so and i transfer the thread to where it belongs.)

I hope this helps.

bakunin
Sponsored Links
    #6  
Old 03-02-2013
hyeewang hyeewang is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 2 March 2013, 9:28 AM EST
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by bakunin View Post
What do you mean by "compile error"? What i wrote was Korn-Shell code. Which programming language are you using? (If not a shell, you are probably in the wrong forum. Say so and i transfer the thread to where it belongs.)

I hope this helps.

bakunin
Hi bakunin,

I am a beginner and only use bash shell. I got your idea and changed it to bash shell. It works. Thank you very much.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get the user input recursively until the user provides valid input i.srini89 Shell Programming and Scripting 1 04-30-2012 03:25 AM
Bash shell - Check if value is valid directory. netmaster Homework & Coursework Questions 2 11-14-2011 05:11 PM
shell script to take input from a text file and perform check on each servers and copy files joseph.dmello Shell Programming and Scripting 0 02-04-2011 07:34 AM
User Input Shell Script sureshcisco Shell Programming and Scripting 3 07-29-2010 07:22 PM
How to check for a valid numeric input Vijayakumarpc Shell Programming and Scripting 1 08-04-2007 08:34 AM



All times are GMT -4. The time now is 10:12 PM.