Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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!

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

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
fithats it
I like case bu what is not turning me on is the Y|y|Yes|YES|yes|YeS|yES|yeS thing heheis there any way arround that?

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

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)

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"
fiyou 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.

#!/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

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!!

![]() |
![]() |
![]() |

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