LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
LinkBack Search this Thread
Old 04-18-2013, 09:10 PM   #1
abrandnewday
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
Scripting help


i need help with the user input, i have to have the user enter two numbers and store them for later use, this is what i have


#!/bin/bash

if [ $# -eq 0 ]
then
echo "Please enter two intergers"
read number number1
fi


if [ $# -ne 2 ]
then
echo "Not enough intergers, please enter two intergers"
read number number1
fi
 
Old 04-18-2013, 10:12 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Rep: Reputation: 282Reputation: 282Reputation: 282
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.

Last edited by shivaa; 04-19-2013 at 08:34 AM.
 
Old 04-18-2013, 10:50 PM   #3
abrandnewday
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
I then need to take number1 and number2 from user imput and have a while loop print the numbers in between number1 and number2
 
Old 04-19-2013, 12:44 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
In that case you need to write some code and see how far you get.
Post back with the code if you get stuck.
I'd advise you to bookmark & read these, they'll give you all the info you need
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 04-19-2013, 01:37 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by abrandnewday View Post
I then need to take number1 and number2 from user imput and have a while loop print the numbers in between number1 and number2
Post your full script, if it's possible. Also mention that where you are stuck at.
 
Old 04-19-2013, 05:24 AM   #6
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 3,860

Rep: Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768
Quote:
Originally Posted by abrandnewday View Post
i need help with the user input, i have to have the user enter two numbers and store them for later use, this is what i have


#!/bin/bash

if [ $# -eq 0 ]
then
echo "Please enter two intergers"
read number number1
fi


if [ $# -ne 2 ]
then
echo "Not enough intergers, please enter two intergers"
read number number1
fi
  • When putting forward code, please use code tags (see shivaa's first post). It is easier to read and just generally helpful.
  • They would be 'integers' and not 'intergers'
  • How do you intend to test that what is entered is (1 or 2) integers, and what do you intend to do if you get other than integer input?
 
Old 04-21-2013, 12:59 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Quote:
Originally Posted by shivaa View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Scripting Help Please sbo Programming 9 02-27-2013 02:06 AM
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Scripting CICA Linux - General 7 10-03-2005 10:36 AM


All times are GMT -5. The time now is 01:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration