Read input and match string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read input and match string
# 1  
Old 11-07-2013
Read input and match string

Code:
#!/bin/ksh

echo DB LIST
ps -ef | grep pmon | grep -v grep | awk -F_ '{print $3}' | sort
db_up=`ps -ef | grep pmon | grep -v grep | awk -F_ '{print $3}' | sort`
echo

echo "Enter database name from the above list"
echo
read ORACLE_SID
echo
echo Database selected is: $ORACLE_SID
echo

#check to make sure SID pass is correct, if not exit
echo $db_up|grep $ORACLE_SID
INPUT_STAT1=$?
if [ "$INPUT_STAT1" = "0" ]
then
# dO NOTHING
echo "Database name selected was good"
else
echo "Database name was NOT selected from the List Provided"
fi

I am trying to create a script that is going to ask the user executing this script a database name, so it can use proper DB on the server and dose other thingi want.
But my trouble is
1. How do i get a user to NOT just hit enter and move along(they need to type something otherwise it keeps prompt for value over and over)
2. Once they do enter something, i want that to match the DB List i provided earlier, i tried coming up with something but there is a big flaw
there. Lets say my DB list comes out as "test dev prod" and once they get to the input part and they just enter p only and not the actual prod,
it will give the good status and say "Ddatabase name selected was good"....it wont go down to the else part....how do i make sure it matches
one of the strings provided in the DB LIST....

Last edited by Don Cragun; 11-07-2013 at 03:52 PM.. Reason: Fix CODE tags.
# 2  
Old 11-07-2013
What is the output from the commands:
Code:
/bin/ksh --version
uname -a

on your system?
# 3  
Old 11-07-2013
you have the code already with read.

Start with this:

Code:
while [ -z "$VALUE_READ" ]
do
read -p 'Enter the DB value: ' VALUE_READ
done

-z - True of the length if "STRING" is zero. Refer here

few points:

Code:
ps -ef | grep pmon | grep -v grep | awk -F_ '{print $3}' | sort
db_up=`ps -ef | grep pmon | grep -v grep | awk -F_ '{print $3}' | sort`

try to use awk instead of grep & pipe.

Code:
echo $db_up|grep $ORACLE_SID
INPUT_STAT1=$?
if [ "$INPUT_STAT1" = "0" ]

you can check the exit status of previous command directly instead of assigning to variable and check.
# 4  
Old 11-07-2013
this would run on different version of HP-UX...mostly pa-risc 11.11 and itanium 11.31

---------- Post updated at 04:56 PM ---------- Previous update was at 04:52 PM ----------

thanks for the info...how would i "check the exit status of previous command directly instead of assigning to variable and check."
# 5  
Old 11-07-2013
You didn't answer my question about which version of the Korn shell you're using. If you have a 1993 or later ksh there is an easy way to do this with arrays. If you have a 1988 vintage ksh, using arrays would be more trouble that it would help. I'll be happy to write one of them for you, but I don't want to take the time to do both this afternoon.
# 6  
Old 11-07-2013
Quote:
Originally Posted by Don Cragun
You didn't answer my question about which version of the Korn shell you're using. If you have a 1993 or later ksh there is an easy way to do this with arrays. If you have a 1988 vintage ksh, using arrays would be more trouble that it would help. I'll be happy to write one of them for you, but I don't want to take the time to do both this afternoon.

Thanks don, i am not able to get the version, it just gives me the prompt back...

host:$ which ksh
/usr/bin/ksh
host:$ /usr/bin/ksh --version
$
$ /usr/bin/ksh --version
$

---------- Post updated at 06:01 PM ---------- Previous update was at 05:23 PM ----------

Quote:
Originally Posted by Don Cragun
You didn't answer my question about which version of the Korn shell you're using. If you have a 1993 or later ksh there is an easy way to do this with arrays. If you have a 1988 vintage ksh, using arrays would be more trouble that it would help. I'll be happy to write one of them for you, but I don't want to take the time to do both this afternoon.
If we assume that it is 1993, then how would i do this ?
# 7  
Old 11-07-2013
It sounds like it is a 1988 version, but try:
Code:
what /usr/bin/ksh|grep Version

Why did the script you posted start with #!/bin/ksh if your shell is /usr/bin/ksh???
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies

3. UNIX for Dummies Questions & Answers

Search String, Out matched text and input text for no match.

I need to search a string for some specific text which is no big deal using grep. My problem is when the search fails to find the text. I need to add text like "na" when my search does not match. I have tried this command but it does not work when I put the command in a loop in a bash script: ... (12 Replies)
Discussion started by: jojojmac5
12 Replies

4. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

5. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

6. Shell Programming and Scripting

Match string from two file input

Hello all, I have file like this: file 1: aa bb cc dd ee file2: 111 111 111 111 111 111 (2 Replies)
Discussion started by: attila
2 Replies

7. Shell Programming and Scripting

Take input from read and place it a string in another file

Hi, This is most likely a dumb question but I could not find answer to it elsewhere. I'm building a simple menu with case /esac and want to read user's input: Please enter XYZ ; read XYZ How do I take the value of XYZ and insert it as a variable $XYZ in file file.txt ? I may need to... (9 Replies)
Discussion started by: svetoslav_sj
9 Replies

8. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

9. UNIX for Dummies Questions & Answers

Regex to match when input is not a certain string (can't use grep -v)

Hey everyone, Basically, all I'm looking for is a way to regex for not a certain string. The regex I'm looking to avoid matching is: D222 i.e. an equivalent of: awk '!/D222/' The problem is that I use this in the following command in a Bash script: ls ${source_directory} | awk... (1 Reply)
Discussion started by: kdelok
1 Replies

10. Programming

How i can read a long integer from standar input and a string with as many characters as specified..

how i can read a long integer from standar input and a string with as many characters as specified in the number? i thing that i must use the read command ofcourse.... (6 Replies)
Discussion started by: aintour
6 Replies
Login or Register to Ask a Question