Operating System - HP-UX
1834934 Members
2726 Online
110071 Solutions
New Discussion

Re: how to capture a string of characters with ksh

 
SOLVED
Go to solution
MAD_2
Super Advisor

how to capture a string of characters with ksh

Using ksh, how can I capture a string of characters (separated by spaces) typed by a user.

Something like:

echo "Type your notes:\n"
CAPTURE NOTES

There will be 60 characters or less typed by the user.

I am attempting to use the string of characters to be inserted into a table afterwards by using PL/SQL.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
16 REPLIES 16
Sachin Patel
Honored Contributor
Solution

Re: how to capture a string of characters with ksh

I guess it is pretty simple


rubens{sachin}% cat test
#!/bin/ksh
read ans
echo $ans

rubens{sachin}% ./test
this is test
this is test
rubens{sachin}%

Sachin
Is photography a hobby or another way to spend $
James R. Ferguson
Acclaimed Contributor

Re: how to capture a string of characters with ksh

Hi Adam:

# echo "Enter your message"
# read MSG
# echo "You wrote: '${MSG}'"

Regards!

...JRF...
MAD_2
Super Advisor

Re: how to capture a string of characters with ksh

Thanks everyone... That's what I am using to record the string but I believe I must be doing something wrong in the PL because only the first character before the space is being recorded.

I am going to double check all my steps.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
curt larson_1
Honored Contributor

Re: how to capture a string of characters with ksh

if it is just a single line of input, use the read built-in.

read
just by itself will put the input in to the default variable REPLY

read varName
will put the input into the variable varName

a trailing \ at the end a line causes the line to be continued onto the next line. ksh will remove both the \ and newline.
John Poff
Honored Contributor

Re: how to capture a string of characters with ksh

Hi,

The previous suggestions were great, but you might want to add a typeset declaration of your variable so that it will be limited to a certain size. Using JRF's example,

typeset -L60 ans

will limit the ans variable to 60 characters, left justified, with leading blanks removed and trailing blanks added to make it 60 characters.

JP
MAD_2
Super Advisor

Re: how to capture a string of characters with ksh

Thanks everyone for your replies. Very strange though, when I pass the value of the string to the PL/SQL code it only records the first character before the space, everything else is not recorded.

In the shell script:
============================
echo "Please include your comments below (60 char max):\n"

read NOTES

sqlplus -s $LOGIN @zipadd_tst.pl $ZIP $SERVICE $BRANCH $USER '${NO
TES}'
============================

At the read, the user inputs something like "This is a test"

But when reviewing the table, the only value recorded is "This"
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
James R. Ferguson
Acclaimed Contributor

Re: how to capture a string of characters with ksh

Hi (again) Adam:

Surround your variable with double-quotes:

# ... "${NOTES}"

Regards!

...JRF...
John Poff
Honored Contributor

Re: how to capture a string of characters with ksh

Hi,

Your sqlplus statement is seeing the $NOTES variable as more parameters and it just getting the first value. Try quoting the $NOTES variable like this:

sqlplus -s $LOGIN @zipadd_tst.pl $ZIP $SERVICE $BRANCH $USER \"${NOTES}\"

JP

MAD_2
Super Advisor

Re: how to capture a string of characters with ksh

Right on the nail, James and John... Thanks a bunch!
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
MAD_2
Super Advisor

Re: how to capture a string of characters with ksh

John (or anyone who may know the answer), another quick question for you regarding my case.

My main problem is resolved, how can I make typeset not leave any trailing spaces, I don't care for additional dead space. I only want recorded what is typed, nothing else, even if I limit it to 60 characters.

Thanks!
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Sachin Patel
Honored Contributor

Re: how to capture a string of characters with ksh

Adam
How about if you use sed to remove any leading and trailing white space

sed 's/^[ \t]*//;s/[ \t]*$//'

Sachin
Is photography a hobby or another way to spend $
MAD_2
Super Advisor

Re: how to capture a string of characters with ksh

Sachin, I do not think that would be practical here, because the strings go into a variable that are loaded directly into a table, so I do not think sed would be of much use here (unless I am not aware how sed can be used in this case). The only way I can think of using sed in your example is by first filterid the contents of the variable into a temp file and then loading from there, but that is adding more steps.

As of right now, I have not set limitis to the lenght of the string, so whatever characteres are typed, that's the only thing that is loaded (typeset is not used), just what I want, but as John has said, the user may type beyond the screen's width (not really recommended) and other limitations. But if I was to set limits with typeset (60 characters is practical, furthermore is the lenght of the field, a varchar2(60)), I don't want trailing spaces, I still only want what is typed to be loaded on the field.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
John Poff
Honored Contributor

Re: how to capture a string of characters with ksh

Hi again,

The typeset functions in ksh [and posix sh] pad the field with trailing spaces. There isn't any option for typeset to tell it to strip out the spaces or not pad the field.

You could use Sachin's suggestion with sed to strip off the trailing spaces. The Korn shell is nice, but the deeper you get into trying to solve problems like this, the closer you get to needing Perl.

JP
MAD_2
Super Advisor

Re: how to capture a string of characters with ksh

I think I will look into resolving the padding with dead space within the PL/SQL code instead of trying to resolve it within the shell script. It appears it will be easier to handle it from there.
Thanks!
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Sachin Patel
Honored Contributor

Re: how to capture a string of characters with ksh

Hi Adam,
It is very simple

#!/bin/sh

typeset -L10 ans
read ans

echo "TTT${ans}TTT"

ans2=`echo ${ans} | sed 's/^[ \t]*//;s/[ \t]*$//'`

echo "TTT${ans2}TTT"

And when I run this script

hpc-ps3# ./ttt
12345678
TTT12345678 TTT (output without truncat)
TTT12345678TTT (after using sed)

Sachin
Is photography a hobby or another way to spend $
MAD_2
Super Advisor

Re: how to capture a string of characters with ksh

Great Sachin, I think I can use this. All my major problems are now fixed, everything else is just semantics.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with