Computing.Net > Forums > Unix > Input from Keyboard

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.

Input from Keyboard

Reply to Message Icon

Name: NewbieKsh
Date: January 1, 2007 at 23:32:53 Pacific
OS: Unix
CPU/Ram: 1 gig
Product: Toshiba
Comment:

I know this is probably the dumbest question but I have no idea how to get the keyboard input into my shell. I am writing a script to take a positive integer, and only one integer from the keyboard input and put it into a while loop in my script. I know this should be simple, probably so simple that no one has bothered to explain it. This is my script so far:

read number #this my keyboard input?????
x= "number"
while (($x>0))
do
print $x
(($x=x-1))
done

thanks in advance for your help
This is my first script in Unix, so please be patient with my lack of knowledge.




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: January 2, 2007 at 08:51:30 Pacific
Reply:

Your problem is probably the while loop:

#!/bin/ksh

# no error checking
echo "enter 1 integer"
read x

while [ $x -gt 0 ]
do
echo $x
#x=$((x-1))
((x-=1))
done

optionally, you can use:
x=$((x-1))

instead of

((x-=1))


0

Response Number 2
Name: NewbieKsh
Date: January 2, 2007 at 09:05:52 Pacific
Reply:

Thanks so much. I am learning from your script! I need to do a lot more to it but this really helps!


0

Response Number 3
Name: NewbieKsh
Date: January 4, 2007 at 10:54:14 Pacific
Reply:

Ok, I have one other question. How do I get my output all on one line. I would like my numbers to read 4, 3, 2, 1 not
4,
3,
2,
1
any ideas?
thanks
Tina


0

Response Number 4
Name: nails
Date: January 5, 2007 at 07:26:56 Pacific
Reply:

One way is using printf:

#!/bin/ksh

# no error checking
echo "enter 1 integer"
read x

while [ $x -gt 0 ]
do
if [[ $x = 1 ]]
then
printf "%d\n" $x
else
printf "%d," $x
fi
((x-=1))

done
# end script

You can also use echo and suppress the newline.


0

Response Number 5
Name: NewbieKsh
Date: January 5, 2007 at 15:26:09 Pacific
Reply:

thanks the printf command did the trick! I am very new to unix!
thanks again
Tina


0

Related Posts

See More



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 Unix Forum Home


Sponsored links

Ads by Google


Results for: Input from Keyboard

Taking input from file and user www.computing.net/answers/unix/taking-input-from-file-and-user/6752.html

how can i get the input from user www.computing.net/answers/unix/how-can-i-get-the-input-from-user/5641.html

input to a program from a file www.computing.net/answers/unix/input-to-a-program-from-a-file/5021.html