Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
clear
echo;echo;echo
echo -n Enter something:' '
read x
if [ "$x" == "time" ]
then
`date`
elif [ "$x" == "Directory" ]
then
`pwd`
elif [ "$x" == "Who" ]
then
`who`
elif [ "$x" -le "9" ]
then
echo You entered a single digit number
else
echo You entered a number elif [ "$x" == "Quit" ]
then
echo Something
fi
if [ "$x" == "time" ]
then
`date`
elif [ "$x" == "Directory" ]
then
`pwd`
elif [ "$x" == "Who" ]
then
`who`
elif [ "$x" -le "9" ]
then
echo You entered a single digit number
else # else needs to be last in the chain (if then elif then elif then else fi)
echo You entered a number
elif [ "$x" == "Quit" ] # see previous comment.
then
echo Something
fi
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
1) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.
2) When doing simple pattern matching on a variable string, a case statement is generally better to use. Unfortunately you can't do arithmetic comparisons in them (not directly, at least), but you can usually include a separate if statement either as a sub-command or as a separate one before or after it.
3) Finally, the shell has built-in variables for $PWD, $HOME, $USER and several others, so there's often no need for the external commands. See the bash man page.
Code:
clear
echo;echo;echo
echo -n Enter something:' '
read x
case ${x,,} in #the "${var,,}" pattern forces the expansion to lowercase
#simplifying the pattern matching
quit)
echo "goodbye"
quit
;;
time)
date
;;
directory)
echo "$PWD"
;;
who)
echo "$USER"
;;
*[^0-9]*) #the string contain non-digit characters
echo "you entered an unknown text string"
;;
[0-9]*)
if (( $x <= 9 )); then
echo "You entered a single digit number"
else
echo "You entered a multi-digit number"
fi
;;
esac
thanks for the help. The reason I am using [] is because that is how my professor is teaching us right now. The help though is much appreciated and if I have more questions, I will keep posting.
clear
echo;echo;echo
echo -n Enter something:' '
read x
if [ "$x" == "Directory" ]
then
pwd
elif [ "$x" == "Date" ]
then
date
elif [ "$x" == "Who" ]
then
who
elif [ ${#x} -gt 20 ]
then
echo "Stop over achieving, stick with less than 20 characters"
elif [ "$x" == "Time" ]
then
date +%H:%M
elif [ "$x" == "Quit" ]
then
its not the finished code...i could have sworn I saved it last time i edited it. well, i'll fix it later...anyways, everything here works
Last edited by druadunc91; 01-28-2013 at 09:08 PM.
Re post #21, That's not the way to properly indent your code. The idea is that all commands that occur at the same level of execution line up at the same level of indentation.
if, elif, else, and fi are all part of the same complex command enclosure, and so should line up, with the sub-commands that it executes under those conditions indented an extra level.
In addition, many people prefer to put the then keyword on the same line as the if, since it's not an independent command, but paired with the previous keyword to bracket the test commands. It's how the shell knows where the test ends and the subcommands start. Putting them on the same line helps keep this visually clear and separate from the sub-commands (this can be relaxed if the test commands themselves occupy multiple lines).
This suggestion also applies to the do keyword in for/while loops.
Pay attention to vertical spacing too, which helps to visually define blocks that go together.
And finally, get into the habit now of commenting your code clearly as you're writing. You'll be very thankful for them when you start reviewing old scripts you wrote and start wondering what the heck you were thinking when you wrote it!
clear
echo;echo;echo
echo -n Enter something:' '
read x
if [ "$x" == "Directory" ]; then
pwd
elif [ "$x" == "Date" ]; then
date
elif [ "$x" == "Who" ]; then
who
elif [ ${#x} -gt 20 ]; then
echo "Stop over achieving, stick with less than 20 characters"
elif [ "$x" == "Time" ]; then
date +%H:%M
else
if [ .... ]; then
acommand
else
bcommand
fi
fi
I fleshed it out at the end with an example of a sub-test to further illustrate proper nesting.
As I said before, a case statement would still be much better here, however, and the double-bracket tests.
Last edited by David the H.; 01-31-2013 at 01:01 PM.
Reason: minor rewording
clear
echo;echo;echo
echo -n Enter something:' '
read x
if [ "$x" == "Directory" ]
then
pwd
elif [ "$x" == "Date" ]
then
date
elif [ "$x" == "Who" ]
then
who
elif [ ${#x} -gt 20 ]
then
echo "Stop over achieving, stick with less than 20 characters"
elif [ "$x" == "Time" ]
then
hour="$(date +%H)"
if [ "$hour" -ge "7" ] && [ "$hour" -le "12" ]
then
echo Morning
elif [ "$hour" -ge "12" ] && [ "$hour" -le "17" ]
then
echo Afternoon
elif [ "$hour" -ge "17" ] && [ "$hour" -le "20" ]
then
echo Evening
elif [ "$hour" -ge "21" ] || [ "$hour" -le "4" ] && [ "$hour" -ge "0" ]
then
echo Night
elif [ "$hour" -ge "4" ] && [ "$hour" -le "7" ]
then
echo
fi
elif [ "$x" == "Quit" ]
then
echo "Bitch"
elif [ "$x" == "Quit" ]
then
echo "Bitch"
elif [[ $x =~ ^-?[0-9]+$ ]]
then
if [ $x -ge 0 ] && [ $x -le 9 ] && [ ${#x} -eq "1" ]
then
echo "You entered a single digit number"
else
echo "You have entered a number"
fi
else
echo "$x"
fi
Here is the finished product. Everything worked the way it should and now, for my next assignment, I do exactly what I did here, but with case statements and not if statements
im back again. Some more help would be appreciated
For this next assignment, I'm taking my finished code and instead of using if's and elif's, I'm using case statements to make the program do exactly as my last one.
this is what I have
Code:
clear
echo;echo;echo
echo -n Enter something here: " "
read x
case "$x" in
Directory)
pwd
;;
Date)
date
;;
Who)
who
;;
esac
right now, i'm trying to get the program to tell the user if they entered more than 20 characters that it is too many.
this is what I tried
Code:
clear
echo;echo;echo
echo -n Enter something here: " "
read x
case "$x" in
Directory)
pwd
;;
Date)
date
;;
Who)
who
;;
${#x} -gt 20)
echo "Over achiever!!!"
;;
esac
as with what i did before where I had [ ] around that last statement with the if's and elif's, do I need the brackets around the that line or what is wrong with it to say they entered more than 20 characters?
Last edited by druadunc91; 02-04-2013 at 04:54 PM.
As mentioned before, a case statement takes the input string and compares it to a list of globbing patterns. The first pattern that matches in the list has its commands executed. Each potential match is equivalent to a "[[ $var == *pattern* ]]" test.
The only way you could directly use a pattern match to calculate "more than/less than" would be something like this:
Code:
case $text in
?|??|???|????) echo "Contains less than 5 characters" ;;
?????????????????????*) echo "Contains more than 20 characters" ;;
*) echo "Contains 5-20 characters" ;;
esac
You have to match an exact number of characters and use logical thinking to output the answers you want.
An easier technique may be to match the length of the string directly, within a range of numbers:
Code:
case ${#text} in
[0-9]) echo "0-9 characters" ;;
1[0-9]) echo "10-19 characters" ;;
2[0-9]) echo "20-29 characters" ;;
*) echo "30 or more characters" ;;
esac
Note finally that bash has extended globbing, which can give you even more matching flexibility.
In any case, druuna's example is certainly the way to go here. greater than/less than style matches really need bracket tests. But here again I would use an arithmetic evaluation instead.
Code:
(( ${#var} > 20 )) && echo "Contains more than 20 characters" ))
PS: your code is looking better. Keep up the good work!
Last edited by David the H.; 02-09-2013 at 08:31 AM.
Reason: minor rewording & code changes
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.