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.
What exactly you want to do and where you're stuck?
In your case, read will not store two input numbers, so use 2 read statements for each number as:
Code:
#!/bin/bash
if (( $# -eq 0 ))
then
echo "Please enter first integer"; read number1
echo "Please enter second integer"; read number2
fi
if (( $# -ne 2 ))
then
echo "Not enough intergers, please enter two integers"
echo "Not enough intergers, please enter first integer"; read int1
echo "Not enough intergers, please enter second integer"; read int2
fi
FYI, $# holds number of arguments specified on command line.
What exactly you want to do and where you're stuck?
In your case, read will not store two input numbers, so use 2 read statements for each number as:
This is incorrect. read can indeed store multiple values at once, with the individual words split by the current value of IFS. If you enter fewer words than the number of variables, then the extra variables will be empty, and if you enter more, then the final variable will contain all the extra text. Use the "-a" option to create an array that will expand to hold as many words as entered.
Code:
read -r -p 'Enter two words: ' word1 word2
echo "Word 1 is \"$word1\""
echo "Word 2 is \"$word2\""
IFS=: read -r -p 'Enter three words, separated by colons: ' word1 word2 word3
echo "Word 1 is \"$word1\""
echo "Word 2 is \"$word2\""
echo "Word 3 is \"$word3\""
read -r -p 'Enter as many words as you want: ' -a words
for ((i=0; i<${#words[@]}; i++ )); do
echo "Word $(( i + 1 )) is \"${words[i]}\""
done
However, using multiple reads does let you provide more feedback to the user and hopefully control the input a little better.
Finally, prompts for user input are often embedded in continual loops so that you can ensure that you're getting what you want before you use it.
Code:
while true; do
read -r -p 'Enter a single lower-case word: ' word
case $word in
*[^a-z]*) echo 'The word must only contain "a-z". Try again.'
continue
*) echo "Entry accepted. You chose \"$word\""
break ;;
esac
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.