- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Bourne Shell Programming Input and Output
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 03:09 AM
10-04-2002 03:09 AM
The output:
echo "Enter number : [0000]"
read inputNumber
Just assume that the 4 zeros inside the bracket is the input that i want to enter. Example, 1111 or 2341. But if follow the source code on top, i can enter more than 4 numbers maybe 12345 or 99999. How to make it only accept 4 numbers or alphabets on the standard output (displayed on the monitor) and standard input (key in using keyboard)? Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 03:11 AM
10-04-2002 03:11 AM
Re: Bourne Shell Programming Input and Output
Regards,
Trond
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 03:35 AM
10-04-2002 03:35 AM
Re: Bourne Shell Programming Input and Output
#!/usr/bin/sh
ans=x
while `perl -e"exit '$ans'=~m/^\d{1,4}$/"` ; do
echo "Gimme a number [0000]\c"
ans=`line`
done
echo "You entered a valid number: $ans"
# sh xx.sh
Gimme a number [0000]a
Gimme a number [0000]12345
Gimme a number [0000]99
You entered a valid number: 99
#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 04:39 AM
10-04-2002 04:39 AM
Re: Bourne Shell Programming Input and Output
#!/bin/sh
while [ true ] ; do
unset ans
echo "Gimme a number [0000]\c"
ans=`line`
anslen=` echo $ans | wc -c `
# echo "ans " $ans $anslen
if [ $anslen -gt 1 -a $anslen -lt 6 ]; then
break
fi
done
echo "You entered a valid number: $ans"
Gimme a number [0000]
Gimme a number [0000]12345
Gimme a number [0000]123
You entered a valid number: 123
As this is for up to 4 digits or alphabets
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 04:49 AM
10-04-2002 04:49 AM
Re: Bourne Shell Programming Input and Output
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 05:12 AM
10-04-2002 05:12 AM
Re: Bourne Shell Programming Input and Output
You need to either write your own little C code (very short code) or use perl (shorter code)
Regards,
Shannon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 05:12 AM
10-04-2002 05:12 AM
Re: Bourne Shell Programming Input and Output
Best lower your expectations rather than spend ages working around them!
Share and Enjoy! Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 05:49 AM
10-04-2002 05:49 AM
Re: Bourne Shell Programming Input and Output
Here's another way, using shell features:
#!/usr/bin/sh
echo "enter a number"
read X
if [ -z "$X" -o `expr length "$X"` -gt 4 -o `expr "$X" : '[0-9,\-]*'` -ne `expr
"$X" : '.*'` ]
then
echo "not a valid number!"
else
echo "ok, it's a good number!"
fi
exit 0
The above piece of code will verify that the input read isn't null; consists only of the digits 0-9; and is only one to four digits in size.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 09:08 AM
10-04-2002 09:08 AM
Re: Bourne Shell Programming Input and Output
1st example,
Enter a number: [____] (4 numbers)
2nd example,
Enter a number: [1234]
3rd example,
Enter a number: [12345 (the bracket is overwritten)
4th example if i backspace,
Enter a number: [1234 (the bracket is lost)
How to maintain the 4 digit to be entered to the system? Only allowed 4 and not more than that. If i press additional key
5th example,
Enter a number: [1234]
--if i press 8
6th example,
Enter a number: [1238]
As you can see the bracket is not overwritten and the user can only enter 4 numbers only. Any idea on this? Can bourne shell do something like this? Thanks for your posting and your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2002 01:33 PM
10-04-2002 01:33 PM
Re: Bourne Shell Programming Input and Output
To Shannon Perty
-----------------
You can not grab a spefic amount of characters in any of the standard shells (borne, korn, csh, tcsh, bash, bash2, etc...).
You need to either write your own little C code (very short code) or use perl (shorter code)
Can you show me an example of c or perl code that enable to grab a specific amount of characters? If i write something in C or Perl, can i integrate that script into my bourne shell application.
Ian Denisson
-------------
Thank you for your advice and i will try to bear that in mind.
Anyway thanks for everyones help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2002 02:04 AM
10-06-2002 02:04 AM
Re: Bourne Shell Programming Input and Output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2002 08:10 AM
10-06-2002 08:10 AM
Re: Bourne Shell Programming Input and Output
Jean-Luc copies my answer - removes the perl part - and gets five point, though we still don't know what is wrong with either.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2002 09:08 AM
10-06-2002 09:08 AM
Re: Bourne Shell Programming Input and Output
Standard Output
---------------
Gimme a number [0000]1234
Gimme a number [0000]12345
Gimme a number [0000]12
..
keep looping
(i did not see the message You entered a valid number ...)
Did you miss out anything?
But what i want is:
Gimme a number [0000] -> 4 numbers only
Gimme a number [12 ] -> i enter 12
Gimme a number [0123] -> i enter 123
Gimme a number [1234] -> i enter 1234
Gimme a number [1235] -> i enter 12345
(i want to enter my input inside the bracket rather than outside the bracket, i know how to position the cursor, but if i enter more than 4 numbers, it overwrite the ])
Base on you reply, It is easy to tweak it to stay within the brackets? Can you explain to me clearly please. Maybe some sample codes. I don't understand your explanation earlier. Anyway thanks for replying. Sorry for the marks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2002 12:52 PM
10-06-2002 12:52 PM
Re: Bourne Shell Programming Input and Output
if you really want to do this in "shell" then you'll have to use something like this:
#!/usr/bin/'sh
# read a 4-digit number from stdin
digit() {
d=$(dd bs=1 count=1 2>/dev/null)
echo $d
}
old=$(stty -g)
stty raw -echo -echoe -echok
number="xxxx"
while [ "$number" -lt "0000" -o "$number" -gt "9999" ]; do
echo "Enter number: \c"
d1=$(dd bs=1 count=1 2>/dev/null); echo "$d1\c"
d2=$(dd bs=1 count=1 2>/dev/null); echo "$d2\c"
d3=$(dd bs=1 count=1 2>/dev/null); echo "$d3\c"
d4=$(dd bs=1 count=1 2>/dev/null); echo "$d4"
number="$d1$d2$d3$d4"
done
stty $old
echo "Your number is $number"
HTH,
Wodisch
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2002 04:13 PM
10-06-2002 04:13 PM
Re: Bourne Shell Programming Input and Output
Stout
-----
script interpreter "/usr/bin/'sh" not found
Script intepreter "/usr/bin/'sh" not found
sh: readnumber.sh not found
(i name your script into readnumber.sh)
So i remove the ' to become /usr/bin/sh
Is it okay to remove it. I think it shouldn't be a problem.
But after i run again your script i saw this error:
stout
-----
readnumber.sh[12]: xxxx: The specified number is not valid for this command.
Your number is xxxx
I analyze your script i notice that can i really compare xxxx with numbers. So i try to change number=1234 or number=11111. But it did not go through the loop.
Can you really run this script properly in unix as i am using HP-UX 10.20? Thank you for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2002 04:32 PM
10-06-2002 04:32 PM
Re: Bourne Shell Programming Input and Output
Stout
-----
Enter number : 1234 (after enter 4 automatic display)
Your number is 1234
But in between the first message and 2nd message there are a lot of spaces.
Stout
------
Enter number : 1234 (spaces) -line 1 - Message 1
(spaces) Your number is 1234 - line 2 - Message 2
1) Can i remove all this spaces so that when i display my result the message 2 will be on the left side.
Expected stout
--------------
Enter number : 1234
Your number is 1234
2) When i just press 4 it just automatic display the message 2? Can i pause it before i display the message 2? Example
Expected stout
---------------
Enter number : 1234 (cursor blinking in 4 except i press enter to move to message 2)
Enter number : 1235 (if i enter 5 it will replace 4, i feel like this looks more realistic).
3) What does this command means?
a. stty raw -echo -echoe -echok
b. stty -g
As i know stty -echo is to hide the input that you enter. What is raw and -echoe and -echok for? stty -g is for what purposes?
Anyway thanks for your help Wodisch. It really help me a lot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2002 08:55 PM
10-06-2002 08:55 PM
Re: Bourne Shell Programming Input and Output
Example 1 (stout)
-----------------
Enter number : 12 + (2 spaces)
Your number is 12
(can i type 12 and press enter to display the second message)
Example 2 (stout)
-----------------
Enter number : 12 + (2 enters)
Your number is 12
(can i only press 1 enter rather than 2 times to see message 2)
Example 3 (stdout)
------------------
Enter number 12 + (2 backspaces)
readnumber.sh[12]: 12^H^H: Syntax error
Your number is 12
(can i press backspaces if i key in the wrong number to modify the new value, but still limits to 4 characters)
Before:
Enter number : 132
--after backspace 2 times
After:
Enter number : 122 (new value)
Example 4 (stdout)
------------------
Enter number : (4 backspaces)
The cursor is below character r
readnumber.sh[12]: ^H^H^H^H: Syntax error
(Is it possible if i press backspace without entering any characters, it will not move back and erase my 1st message)
Before:
Enter number : _ (_ = cursor)
--after press backspace
After:
Enter number : _ (still at the same
It sound confusing here but i do not know whether bourne shell programming can do that or not? Any bourne shell sample codes to aid me thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2002 08:57 AM
10-13-2002 08:57 AM
Re: Bourne Shell Programming Input and Output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2002 09:54 AM
10-13-2002 09:54 AM
SolutionYou *do* have to use C/perl/... to get into more shiny processing, and use libraries or modules to do extensive user interaction. C can be used to build user interfaces that adopt to X11, Motif, KDE, Qt, or any `standard' or well documented graphic frontend. Perl has a wide range of modules that interface to standard graphic environmets, like Perl/Tk and Perl/Gtk.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2002 04:25 PM
10-13-2002 04:25 PM
Re: Bourne Shell Programming Input and Output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2002 04:50 PM
10-13-2002 04:50 PM
Re: Bourne Shell Programming Input and Output
http://www.webmin.com/hpux.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2002 04:58 PM
10-13-2002 04:58 PM
Re: Bourne Shell Programming Input and Output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2002 01:52 AM
10-14-2002 01:52 AM
Re: Bourne Shell Programming Input and Output
Just for fun ... with this one you can use backspace and can't enter more than $MAX chars (this could also be a parameter). I also added a filter to accept only digits and '.'.
Regards,
Jean-Louis.
#!/usr/bin/sh
beep()
{
VAR=
echo '\007''\c'
}
STRING=$1
LEN=$(expr "$1" : ".*")
MAX=4
OLDSTTY=$(stty -g)
FORMAT1="\rEnter number ["
FORMAT2="${FORMAT1}%${MAX}s]"
VAR=
while [ -z "$VAR" ]
do
printf "$FORMAT2" " "
printf " "
printf "$FORMAT1$STRING"
stty raw -echo -echoe -echok
VAR=$(dd bs=1 count=1 2>/dev/null)
stty $OLDSTTY
INP=$(echo $VAR | od -c | awk '{print $2}')
[ "$INP" = "\\b" ] &&
{
case "$LEN" in
0 ) beep ; continue ;;
1 ) STRING= ;;
* ) STRING="$(echo $STRING | cut -c1-$(($LEN - 1)))" ;;
esac
exec $0 "$STRING"
}
[ "$INP" = "\\r" ] &&
{
echo "\n\nInput number was '$STRING'\n"
exit 0
}
[ $LEN -ge $MAX ] &&
{
beep
continue
}
VAR=$(echo $VAR | tr -cd '[0-9.]')
[ -z "$VAR" ] && beep
done
STRING="$STRING$VAR"
exec $0 $STRING
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2002 02:19 AM
10-14-2002 02:19 AM
Re: Bourne Shell Programming Input and Output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2002 02:59 AM
10-14-2002 02:59 AM
Re: Bourne Shell Programming Input and Output
1) What if i want to enter alphanumeric how do i validate in your script?
2) If there is more than 2 inputs:
Enter number : [0000] (4 numbers)
Enter name : [aaaaaaaaaaa] (11 alphabets)
Do i need to do validations for both the inputs or ?
3) If there is more than 10 inputs using this method, will it increase the unix uptime?
4) Which line of code disable the usage of crtl + c, ctrl + d, etc?
Thank you Jean Louis Phelix.