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
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.
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
Summary: Folks, I have a while loop that is taking input from a file while read variable do . . . done < filename.txt However during the loop I am trying to take some user input print "....[y/n]" read $va...
Summary: Dear Friend, could anyone help to write a shellscript that take the input from the user and search in the files, then print is out. For example: that the user want to print line 1 to 5 of the text fil...
Summary: Hello all, I hav a c program which does a scanf. its like this :- int main() { int i; scanf("%d",i); return 0; } Is it possible 4 the program to take input from a file & not wait 4 the user in...