Computing.Net > Forums > Linux > shell script help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

shell script help

Reply to Message Icon

Name: MrJake
Date: October 22, 2002 at 18:46:07 Pacific
OS: Mandrake 9.0
CPU/Ram: p3-800/512
Comment:

Im starting to learn bash. i lvoe it. im trying to do something but want the easy way arround.

I made this script:

#!/bin/bash
#Author = MrJake
#
#Desc = will display a chmod list
#
#
#Created on Mon Oct 21 20:58:00 EDT 2002
#
echo "Chmod help by MrJake"
listagain () {
echo -n "Need help again (y/n) : "
read ANSWER
if [ "${ANSWER}" = "y" ]; then
echo "Here we go again";
sleep 1;
askuser;
else
exit 1
fi
}

noinput () {
echo "Not a valid input";
echo "Please start again"
echo ""
}
askuser () {
echo "CHMOD List"
echo ""
echo "Please select one of the following"
echo "1 list user"
echo "2 list group"
echo "3 list other"
echo "h for help"
read CHMODLIST
CL=${CHMODLIST}
echo ""
if [ "${CL}" = "1" ]; then
echo "Chmod Permissions for user"
echo --"
echo "100 ---x------";
echo "200 --w-------";
echo "300 --wx------";
echo "400 -r--------";
echo "500 -r-x------";
echo "600 -rw-------";
echo "700 -rwx------";
echo "";
listagain
elif [ "${CL}" = "2" ]; then
echo "Chmod Permissions for group"
echo ---"
echo "010 ------x---";
echo "020 -----w----";
echo "030 -----wx---";
echo "040 ----r-----";
echo "050 ----r-x---";
echo "060 ----rw----";
echo "070 ----rwx---";
echo "";
listagain
elif [ "${CL}" = "3" ]; then
echo "Chmod Permissions for other"
echo ---"
echo "001 ---------x";
echo "002 --------w-";
echo "003 --------wx";
echo "004 -------r--";
echo "005 -------r-x";
echo "006 -------rw-";
echo "007 -------rwx";
echo "";
listagain
elif [ "${CL}" = "h" ]; then
echo "chmod help";
echo "d = dir"
echo "r = read"
echo "w = write"
echo "x = execute"
echo "";
listagain
elif [ "${CL}" = "help" ]; then
echo "chmod help";
echo "d = dir"
echo "r = read"
echo "w = write"
echo "x = execute"
echo "";
listagain
else
noinput;
askuser;
fi
}

askuser


Now where i need help is with the input from the user. If the user enters y for askagain function then it will go and call askuser function. But if he replies andthing else including nothing or n or whatever it will exit. now the problem is this. How do i make it that if the user enters Y or Yes or yes or YES instead of y is there a way for me to make it so that his input is forced to y if it starts with Y or y?
the only way i found was with if, elif but man this can make it long. What if i fall upon some very dumb user that types YeS then im screwed hehe.

Any suggestion that will fix my issue is welcomed with great smile :)

remember im a real noobie at bash or any shell scripting. please bare with me hehe.

thanks



Sponsored Link
Ads by Google

Response Number 1
Name: slightlyburntout
Date: October 22, 2002 at 22:44:37 Pacific
Reply:

I'm just learning BASH myself, but I'm not writing anything like that. "make clean" blew me away! LMAO! I'm going to follow this thread. Folks, before you leave, please remember to piss all over anything pro-TCPA or pro-M$ Palladium. Thanks!


0

Response Number 2
Name: Danny Larouche
Date: October 23, 2002 at 06:54:11 Pacific
Reply:

You should use "case" instead

echo "yes or no? "
read vari
case "$vari" in
n|N|NO|no)
echo "No!!"
;;
y|Y|yes|YES)
echo "yes!!!"
;;
*)
echo "anything else"
esac


0

Response Number 3
Name: MrJake
Date: October 23, 2002 at 09:24:37 Pacific
Reply:

Well hehe the next thing in my tutorial is about case lol. but the thing is its not exactly what i wanted. its still requiring that i feed it as much info as i can. I was looking for something more like making it format the ionput of user to go along with if

something like
echo -n "Do it again? (y/n) : "
read ANSWER
AS=${ANSWER} #<--im lazy
#this is where i want it to be formatted
#but im lost I know u can play with [a-z][A-Z] thingy
3cant we make it so that if user enters YES it becomes y. or YeS becopmes y. Yoo ignored and No = n etc.. without having to have 6 examples of what to use to make a decision
#then
if [ "${AS}" = "y" ]; then
command1
else
exit 1
fi

thats it
I like case bu what is not turning me on is the Y|y|Yes|YES|yes|YeS|yES|yeS thing hehe

is there any way arround that?


0

Response Number 4
Name: David Perry
Date: October 23, 2002 at 09:46:42 Pacific
Reply:

Get just the first character. translate to lowercase.

var=YeS;
echo $var | awk '{ print substr($1,0,1) }' | tr 'YN' 'yn'


0

Response Number 5
Name: Danny Larouche
Date: October 23, 2002 at 10:11:02 Pacific
Reply:

$AS=`echo $ANSWER|awk '{print tolower(substr($1,1,1))}'`


0

Related Posts

See More



Response Number 6
Name: mrjake
Date: October 23, 2002 at 11:37:34 Pacific
Reply:

Get just the first character. translate to lowercase.
var=YeS;
echo $var | awk '{ print substr($1,0,1) }' | tr 'YN' 'yn'

sounds good to me David but what does it mean ?


but Danny i think i understand yours. im just not familiar with awk
$AS=`echo $ANSWER|awk '{print tolower(substr($1,1,1))}'`
can u explain?

I told you im learning lol. its like telling someone well solidfuel is simple. do this. 738ab*5%/38.2938 to 3827* will result in 84.384%/374/32.38475 (this is a BS command to explain how I dont understand the command u suggest hehe)



0

Response Number 7
Name: David Perry
Date: October 23, 2002 at 13:42:06 Pacific
Reply:

MrJake,

The part
echo $var | awk '{ print substr($1,0,1) }'
says to take just the first character of $var
The next part
| tr 'YN' 'yn'
says to translate a 'Y' to 'y' and 'N' to 'n'
although I like the tolower option of awk better.

so if your script looked like

echo "Do you want world peace [Y|n]";
read answer
if [ `echo $answer | awk '{ print substr($1,0,1) }' | tr 'YN' 'yn'` = "n" ] ; then
echo "war"
else
echo "peace"
fi

you could read any answer with the default being "y".

The tr command will translate any character or series of characters to something else.

The substr command in awk lets you work with a portion of a text string.

hope this helps.


0

Response Number 8
Name: jwalk
Date: October 23, 2002 at 16:15:26 Pacific
Reply:

#!/bin/bash
echo "Do you want some cake (y/n):"
read ANSWER
SUCCESS=`echo "$ANSWER" | egrep -i
'^yes$|^y$'`
if [ "$SUCCESS" != "" ]; then
echo "You want cake"
else
echo "You don't want cake"
fi


0

Response Number 9
Name: jwalk
Date: October 23, 2002 at 16:17:58 Pacific
Reply:

in that last post, this line:

'^yes$|^y$'`

should be on the previous line...


0

Response Number 10
Name: Danny Larouche
Date: October 23, 2002 at 17:40:48 Pacific
Reply:

Hi MrJake!!

Awk is a very powerful commands (even a language by itself). What it is doing in david's example and mine, it take the first input from $1 and keep the first character and finally convert it to either lower or uppercase.

I also assigned the result to the variable $AS using the strange ````` (located at left of [1] key and below [Esc]. NOTE: ` and ' and not the same thing!!



0

Response Number 11
Name: mrjake
Date: October 23, 2002 at 19:32:53 Pacific
Reply:

Hey guys that was great help. Thanks big time.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: shell script help

advanced shell script help www.computing.net/answers/linux/advanced-shell-script-help/17749.html

begginner shell script help ASAP! www.computing.net/answers/linux/begginner-shell-script-help-asap/18683.html

Shell Script - help www.computing.net/answers/linux/shell-script-help/21347.html