|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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"
fiI hope this helps. bakunin |
| The Following User Says Thank You to bakunin For This Useful Post: | ||
hyeewang (03-02-2013) | ||
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
I hope this helps. bakunin |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|