Check User Input String in array with case in esac? - case

I am trying to write a ksh shell script in bash to check a user entered string is equal to the element stored in array.
loc_ora=/etc/oratab
osid=`sed -nr 's/^(.*):\/.*$/\1/p' $loc_ora`
set -A arr $osid
OIFS=$IFS;
IFS=" ";
read usid
case "${arr[*]}" in
* /$usid/ *)
echo -e " \t\t Entered SID Matches ... \n"
ORACLE_SID=$usid
echo -e " \t\t SID: $ORACLE_SID \n"
export ORACLE_SID
export ORACLE_HOME
;;
*)
sleep 03
echo -e " \t\t Entered SID Does not Match the System !!! \n \t\t Please Re-run the Script with a Valid SID."
sleep 02
echo -e " \t\t ABORTING !!! ..."
sleep 03
exit 0
;;
esac
IFS=$OIFS;
Error/scenario1:
I am facing a syntax error: `/' unexpected if I keep /$usid/.
Error/scenario2:
If I remove the // and keep like *$usid* in the pattern, I do not face any error but only the second *) half pattern is printing even when I provide the correct SID.
How do I do this validation? Please help.
Thanks,
Karthik

I would suggest checking entered uid against /etc/oratab differently:
read uid
loc_ora=/etc/oratab
sed -nr 's/^(.*):\/.*$/\1/p' $loc_ora | grep -q "^$uid\$"
if [ $? -eq 0 ]; then
echo "sid valid"
else
echo "sid Not Valid"
Fi

Related

shell script to find the number of occurence of particular word from text file

My file is ss.txt
Another instance started
Another instance started
Another instance started
Another instance started
No instance started
No instance started
No instance started
No instance started
If i use shell script program as this
#!/bin/sh
t=0
#temp=0
echo "Enter filename"
read f1
if [ -f $f1 ]
then
echo "1.count char,words,lines"
echo "2.particular word to find in file"
echo "3.exit"
echo "Enter ur choice?"
read ch
case $ch in
1) wc -c $f1
echo "characters"
wc -w $f1
echo "words"
wc -l $f1
echo "lines" ;;
2) echo "Enter the word whose occurence has to be found"
read c
t='echo $f1 | wc -c'
echo $t ;;
3) exit ;;
esac
else
echo "File does not exist"
fi
If i run this code i get the following output
i could get the option 2 correct that is word occurence is not correct
i get like
Enter filename
ss.txt
1.count char,words,lines
2.particular word in file
3.exit
Enter ur choice?
1
180 ss.txt
characters
24 ss.txt
words
8 ss.txt
lines
This i get correctly but for choice 2 i get like
Enter filename
ss.txt
1.count char,words,lines
2.particular word in file
3.exit
Enter ur choice?
2
Enter the word whose occurence has to be found
Another
0
See i get zero here but output should be 4
change from:
t='echo $f1 | wc -c'
to
t=`grep -o "$c" "${f1}"|wc -l`
Please try below commands as per your requirement
t=grep -c "text" "${f1}" ( -c, --count count of matching lines for each input file)
or
t=grep -o "text" "${f1}" ( -o,Show only the part of a matching line that matches PATTERN.)
bcsmc2rtese001 [~/Shell_p]$ grep -c "Another" ss.txt
4
bcsmc2rtese001 [~/Shell_p]$ grep -o "Another" ss.txt
Another
Another
Another
Another

How can I set a default value when incorrect/invalid input is entered in Unix?

i want to set the value of inputLineNumber to 20. I tried checking if no value is given by user by [[-z "$inputLineNumber"]] and then setting the value by inputLineNumber=20. The code gives this message ./t.sh: [-z: not found as message on the console. How to resolve this? Here's my full script as well.
#!/bin/sh
cat /dev/null>copy.txt
echo "Please enter the sentence you want to search:"
read "inputVar"
echo "Please enter the name of the file in which you want to search:"
read "inputFileName"
echo "Please enter the number of lines you want to copy:"
read "inputLineNumber"
[[-z "$inputLineNumber"]] || inputLineNumber=20
for N in `grep -n $inputVar $inputFileName | cut -d ":" -f1`
do
LIMIT=`expr $N + $inputLineNumber`
sed -n $N,${LIMIT}p $inputFileName >> copy.txt
echo "-----------------------" >> copy.txt
done
cat copy.txt
Changed the script after suggestion from #Kevin. Now the error message ./t.sh: syntax error at line 11: `$' unexpected
#!/bin/sh
truncate copy.txt
echo "Please enter the sentence you want to search:"
read inputVar
echo "Please enter the name of the file in which you want to search:"
read inputFileName
echo Please enter the number of lines you want to copy:
read inputLineNumber
[ -z "$inputLineNumber" ] || inputLineNumber=20
for N in $(grep -n $inputVar $inputFileName | cut -d ":" -f1)
do
LIMIT=$((N+inputLineNumber))
sed -n $N,${LIMIT}p $inputFileName >> copy.txt
echo "-----------------------" >> copy.txt
done
cat copy.txt
Try changing this line from:
[[-z "$inputLineNumber"]] || inputLineNumber=20
To this:
if [[ -z "$inputLineNumber" ]]; then
inputLineNumber=20
fi
Hope this helps.
Where to start...
You are running as /bin/sh but trying to use [[. [[ is a bash command that sh does not recognize. Either change the shebang to /bin/bash (preferred) or use [ instead.
You do not have a space between [[-z. That causes bash to read it as a command named [[-z, which clearly doesn't exist. You need [[ -z $inputLineNumber ]] (note the space at the end too). Quoting within [[ doesn't matter, but if you change to [ (see above), you will need to keep the quotes.
Your code says [[-z but your error says [-z. Pick one.
Use $(...) instead of `...`. The backticks are deprecated, and $() handles quoting appropriately.
You don't need to cat /dev/null >copy.txt, certainly not twice without writing to it in-between. Use truncate copy.txt or just plain >copy.txt.
You seem to have inconsistent quoting. Quote or escape (\x) anything with special characters (~, `, !, #, $, &, *, ^, (), [], \, <, >, ?, ', ", ;) or whitespace and any variable that could have whitespace. You don't need to quote string literals with no special characters (e.g. ":").
Instead of LIMIT=`expr...`, use limit=$((N+inputLineNumber)).

check if file is empty or not

How do I check if a file is empty in a korn script
I want to test in my korn script if the output CSV file is empty or not and if it is not empty then it should give the count of values.
Thanks.
The test(1) program has a -s switch:
-s FILE
FILE exists and has a size greater than zero
This is just another way of doing it, albeit a roundabout one:
if [ `ls -l <file> | awk '{print $5}'` -eq 0 ]
then
//condition for being empty
else
//condition for not being empty
fi
if [ ! -f manogna.txt ]
then
echo " Error: manogna.txt does not exist "
else
echo " manogna.txt exist "
echo " no of records in manogna.txt are `cat manogna.txt | wc -l`"
fi

How to use sed with user input

Script performs two things
1.Enables the user input a file name
2.Enables the user to input a line number to view the content
echo "Enter the file name"
read fname
find / -name "$fname" > /tmp/newone.txt
if test $? -eq 0
then
{
echo "File found"
echo "The no of line in the file $fname is `cat /tmp/newone.txt | wc|awk '{pri
nt $1}'`"
echo "Enter the line no"
read lcnt
sed '"$lcnt" p' "$fname"
}
else
{
echo "File not found"
}
fi
Issue
1.Getting error in the sed part
Error message "sed: -e expression #1, char 3: extra characters after command"
how to rectify it ?
2.Can i redirect the output of 'find' to a variable
For example
$flloc =/tmp/newone.txt
so i will be able to use '$flloc' instead of the absolute path
1) This is how you'd go about using your variable in the sed command:
echo "Line no: "
read lcnt
sed -n "$lcnt p" $fname
What was wrong with your original expression is that bash variables aren't interpreted when you use single quotes. Example:
lcnt=5
# prints $lcnt
echo '$lcnt'
# prints 5
echo "$lcnt"
2) To store your find output to a variable, simply do this:
floc=`find / -name $fname` # Here I'm using backticks, not single quotes.

shell programming - interpreting use of $? with function call

I'm trying to understand a shell/bash script and just wanted input on the use of $? in the code.
Its being used with a function call.
Function example:
function showerr { err=$1
if [ $err -ne 0 ]; then
echo `date` : "error!"
echo "stat : " $2
echo `date` : "stat: " $2
# alert email
prog=$0
uname=`whoami`
echo `date` : Sending email to ${ADDR_TO}
mailx -s "Error checking status " $ADDR_TO << EOF
+++++++++++++++++++++
stat = $2
util = $prog
host = $uname
+++++++++++++++++++++
Check $uname for details.
.
EOF
echo "Exiting program..."
exit 1
fi
}
Here are some statements calling showerr. I see some within a condition (using values like 1 or any number) and some just calling it $?.
if [[ $Res = *"FileNotFound"* ]]
then
echo `date` : Msg here
showerr 1 "Msg details here"
else
echo `date` : File: <filename> found.
fi
echo `date` : Msg detail here
flsz=`echo $size | cut -d'"' -f2`
showerr $? "error getting size for: (${flsz})"
$? is the exit code from the last command. See Shell Command Language: Special Parameters for the list of such special variables in POSIX shells.
The showerr function logs an error if its first parameter is not 0.
So:
./some_super_script_that_might_fail
showerr $? "SuperScript failed"
will only log something if ./some_super_script_that_might_fail's exit code is not 0 (which traditionally means that it failed).
showerr 1 "message"
will always log.
showerr 0 "message"
will never do anything.

Resources