Operating System - HP-UX
1833832 Members
2364 Online
110063 Solutions
New Discussion

Bourne Shell Programming Input and Output

 
SOLVED
Go to solution
Chua Wen Ching
Regular Advisor

Bourne Shell Programming Input and Output

I need some help here and i not sure whether bourne shell programming can do it or not. I am using hp-ux 10.20.
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.

wenching
44 REPLIES 44
Trond Haugen
Honored Contributor

Re: Bourne Shell Programming Input and Output

I believe that requiers som programming in C or som other language..

Regards,
Trond
Regards,
Trond Haugen
LinkedIn
H.Merijn Brand (procura
Honored Contributor

Re: Bourne Shell Programming Input and Output

# cat xx.sh
#!/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
#
Enjoy, Have FUN! H.Merijn
Jean-Luc Oudart
Honored Contributor

Re: Bourne Shell Programming Input and Output

Without perl :
#!/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
fiat lux
harry d brown jr
Honored Contributor

Re: Bourne Shell Programming Input and Output

It's perl time!

live free or die
harry
Live Free or Die
Shannon Petry
Honored Contributor

Re: Bourne Shell Programming Input and Output

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)

Regards,
Shannon
Microsoft. When do you want a virus today?
Ian Dennison_1
Honored Contributor

Re: Bourne Shell Programming Input and Output

Unfortunately POSIX Shell is not as all-singing and all-dancing as other structured programming languages.

Best lower your expectations rather than spend ages working around them!

Share and Enjoy! Ian
Building a dumber user
James R. Ferguson
Acclaimed Contributor

Re: Bourne Shell Programming Input and Output

Hi:

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...
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

Thank you for posting up the sample source codes on handling the standard input. Thanks a lot. But i had the problem which i will show below:

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.
wenching
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

Oops...i forget to read the messages properly.

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.
wenching
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

Really no solutions? Anyway thanks for your help.
wenching
H.Merijn Brand (procura
Honored Contributor

Re: Bourne Shell Programming Input and Output

What's wrong with my answer? It is easy to tweak it to stay within the brackets.

Jean-Luc copies my answer - removes the perl part - and gets five point, though we still don't know what is wrong with either.
Enjoy, Have FUN! H.Merijn
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

Dear procura, i had tried your sample code but it only displays:
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.
wenching
Wodisch
Honored Contributor

Re: Bourne Shell Programming Input and Output

Hi,

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
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

Dear Wodish, when i run your script i saw an error:
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.
wenching
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

Dear Wodisch, The script can work already as i assign number=-1. But i do not understand why you assign number="xxxx"? But i feel there is some problem right here with the standard 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.
wenching
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

Dear Wodisch, i see errors when i press the backspace, space bar and enter key. Examples:

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.


wenching
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

No other solutions? Is it really that bourne shell does not have the capabilities to do that. Anyway thank you.
wenching
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Bourne Shell Programming Input and Output

The bourne shell ain't no glossy programming environment :) It's the most basic interface the user has to the system.

You *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.
Enjoy, Have FUN! H.Merijn
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

The problem here is my org unix does not have perl/tk and have an old perl interpreter 4.0. I had some problems whether C can work well with bourne/Posix scripting? Is there any C and Bourne/Posix sample scripts (both integrated) to do the processing? Thank you.
wenching
Michael Tully
Honored Contributor

Re: Bourne Shell Programming Input and Output

You can get perl version 5.6.0 for 10.20 at the following link ready for installation.

http://www.webmin.com/hpux.html
Anyone for a Mutiny ?
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

I had send an appeal so that my org can update to perl5.6.0. Thanks for the link. How about the C and Bourne/Posix integration? Thank you.
wenching
Jean-Louis Phelix
Honored Contributor

Re: Bourne Shell Programming Input and Output

Hello,

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

It works for me (© Bill McNAMARA ...)
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

Thank you Jean Louis Phelix a lot. This script really save me a lot. How do you manage to figure it out the logic?
wenching
Chua Wen Ching
Regular Advisor

Re: Bourne Shell Programming Input and Output

The code is excellent where i ctrl + c or esc it is still in the input mode. But can you add some comments as i am quite confuse with the script. Comments can help (if any)

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.
wenching