INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Feedback
"...This was the ONLY place that I could find information that I could use to resolve the problem. So thanks once again to member TomSark and the SQL forum!..."
Geography
Where in the world do Tek-Tips members come from?
|
trouble with controlling loop in script and debugging (5)
|
|
|
ZiggyS1 (Programmer) |
20 Jan 11 10:22 |
hi, I'm having a few problems, I am adapting this script in baby steps, and learning as I go. Right now I have it collecting scans and writing (appending) to a file. What I wanted to do was have a user scan/enter an order number and then all subsequent scans would write to the file with the order number and UPC beside... ORD1 772454051111 ORD1 772454050855 ORD1 883049178936 ORD1 883049128115 the problem is after each scan I want it to hold onto the order # and stay on the barcode field... so I don't lose the Order Variable. I was trying to debug with a counter, but could not get my IF condition to work... just need a little guidance to get me in the right direction. I know there is a lot missing from the code to make it better, but I am only focusing on this portion at the moment. CODE #!/bin/sh #-------------------------------------------- # Author: Ziggy
# Date : January 18 2011 # RF scanning #--------------------------------------------
# Main Variables Definition
LOGO="RF Scan Menu" bold=`tput smso` offbold=`tput rmso` counter=1
#------------------------------------------------------ # MAIN MENU PROMPTS #------------------------------------------------------ amenu="Type exit to close";
#------------------------------------------------------ # MAIN Routine #------------------------------------------------------
# trying to use to debug and count loop... gives error for...bracket } #% if ($counter > 1 ) { #% break #% }
mainframe () { clear tput cup 3 1; echo xxxxxxxxxxxxxxxxxxxxx tput cup 4 1; echo x; tput cup 4 21; echo x tput cup 5 1; echo xxxxxxxxxxxxxxxxxxxxx tput cup 9 5; echo Scan Barcode }
themenu () { tput cup 0 4; echo "${bold} Company Name ${offbold}" tput cup 1 5; echo "${bold} $LOGO ${offbold}\n" tput cup 4 2; echo $amenu; tput cup 9 0; echo $MSG
# position of field to scan into
tput cup 8 3; echo "Order: " tput cup 10 2; echo "Scan: "
tput cup 8 10; read ORDN }
#------------------------------------------------------ # MAIN LOGIC #------------------------------------------------------
#MSG= while true do mainframe themenu tput cup 10 10; read answer
# MSG= case $answer in
exit) clear;break;; *)echo $ORDN $answer >> /home/zigsto/ScanTest2.txt;; esac
counter=$(echo "$counter +1" | bc)
#break
done print $counter
Thanks Ziggy |
|
|
ZiggyS1 (Programmer) |
20 Jan 11 12:16 |
I really need to understand the bracketing thing... eg... Syntax error at line xx : `{' is not expected. I get that if i try to add a IF or While condition/loop I tried both eg CODE i = 1 while ( i <=4 ) { print $i ++i }
|
|
The brackets are for C programs. An "if" in the Bourne Shell would be like this... CODEif ( $counter > 1 ) then break fi Try "man sh" for more information. I would also recommend you use the Korn shell (ksh). It's just personal preference, but I find it does have more features and it's more forgiving (it does what you mean more often). |
|
|
ZiggyS1 (Programmer) |
20 Jan 11 12:49 |
thanks ok I decided to experiment in this area with the if.... CODEif ( $ORDN = "" ) then
tput cup 8 3; echo "Order: "
tput cup 8 10; read ORDN fi now i get this error... what does it mean? Sorry, I'm not very proficient with Unix, I know a little bit here and there.... enough to be dangerous :) rfscan2[11]: ORDN: not found. |
|
|
ZiggyS1 (Programmer) |
20 Jan 11 14:13 |
I am using ksh btw... I know it only said sh at the top. also tried this but this says no matching DO CODE while true do case $ORDN in
1) tput cup 8 3; echo "Order: " tput cup 8 10; read ORDN;;
*) tput cup 8 3; echo "Order: " tput cup 8 10; echo $ORDN;;
esac
arghh... I was doing so good yesterday |
|
|
ZiggyS1 (Programmer) |
20 Jan 11 14:42 |
I figured it out. I will post as soon as I clean it up. |
|
|
ZiggyS1 (Programmer) |
20 Jan 11 15:43 |
here is my solution.... works great..... thanks for you input, got me thinking in the right direction.... most of my problem is not being able to spot my errors quickly. CODE#!/bin/sh #-------------------------------------------- # Author: Ziggy S
# Date : January 18 2011 # RF scanning #--------------------------------------------
# Main Variables Definition
LOGO="RF Scan Menu" bold=`tput smso` offbold=`tput rmso` counter=1 counterb=1 ORDN=1 stmp=`date`
#------------------------------------------------------ # MAIN MENU PROMPTS #------------------------------------------------------ amenu="Type exit to close";
#------------------------------------------------------ # MAIN Routine #------------------------------------------------------
mainframe () { clear tput cup 3 1; echo xxxxxxxxxxxxxxxxxxxxx tput cup 4 1; echo x; tput cup 4 21; echo x tput cup 5 1; echo xxxxxxxxxxxxxxxxxxxxx tput cup 9 5; echo Scan Barcode }
themenu () { tput cup 0 4; echo "${bold} Company ${offbold}" tput cup 1 5; echo "${bold} $LOGO ${offbold}\n" tput cup 4 2; echo $amenu; tput cup 9 0; echo $MSG
# position of field to scan into
tput cup 10 2; echo "Scan: "
# used to either "Read" the order number or display it if it was scanned once # when appending to file (at end of code), the order number needs to remain static for the session case $ORDN in
1) tput cup 8 3; echo "Order: " tput cup 8 10; read ORDN;;
*) tput cup 8 3; echo "Order: " tput cup 8 10; echo $ORDN;;
esac
}
#------------------------------------------------------ # MAIN LOGIC #------------------------------------------------------
while true do # goes to menu routines mainframe themenu tput cup 10 10; read answer
case $answer in
exit) clear;break;; # Appends scans to file *)echo $ORDN","$answer","$stmp >> /home/zigsto/ScanTest2.txt;; esac
done
|
|
feherke (Programmer) |
21 Jan 11 4:07 |
Hi Just to clean up a mistake. Quote (SamBones):if ( $counter > 1 )
Quote (Ziggy):if ( $ORDN = "" )
Syntactically correct, but probably not what you want. Quote (man ksh):((expression )) The expression is evaluated using the rules for arithmetic evaluation described below. If the value of the arithmetic expression is non-zero, the exit status is 0, otherwise the exit status is 1.
(list ) Execute list in a separate environment. Note, that if two adjacent open parentheses are needed for nesting, a space must be inserted to avoid evaluation as an arithmetic command as described above.
SamBone's code will execute the command $counter and redirect its output to the file 1. If $counter has no value, an empty file with name 1 will be created. Ziggy's code will execute the command $ORDN and pass = "" to it as parameters. If $ORDN has no value, will try to use = as command and will fail. But as (( )) is for numeric evaluation only and if I understand correctly $ORDN may contain any string, better use [[ ]] for the test : CODEif [[ "$ORDN" = "" ]] then tput cup 8 3; echo "Order: " tput cup 8 10; read ORDN fi Feherke. http://free.rootshell.be/~feherke/ |
|
|
ZiggyS1 (Programmer) |
21 Jan 11 9:03 |
thanks...feherke, stefanwagner and Sam
I'm reading up on the proper syntax, I have a couple of Unix books by Dale Dougherty/Arnold Robbins... so I'll pay attention to your comments.
One question, would the best practice be for me to create separate programs to "Call" from the main to do different things like other menus or functions |
|
You might be using the Korn Shell, but your script is using the Bourne Shell if is has this line as the first line... That is called the "she-bang line" and forces the script to use that program, whatever it is. It is the only comment in a script that actually gets executed. Change it to this... |
|
|
ZiggyS1 (Programmer) |
21 Jan 11 13:06 |
thanks Sam, I thought the comment was good enough...changed it.
another question...
works... if [[ "$ORDN" = "" ]]
does not work...
if [[ "$ORDN" <> "" ]]
how do I show not equal to blank
|
|
|
ZiggyS1 (Programmer) |
21 Jan 11 13:14 |
ahh ok I found it...
if [[ "$ORDN" -ne "" ]] |
|
|
ZiggyS1 (Programmer) |
21 Jan 11 13:59 |
ok, another glitch...
using.. if [[ "$ORDN" -ne ""]]
seems to not allow me to use characters, which makes sense as noted by feherke... but using braces doesn't work right as it gets stuck in a loop... this works perfect fro numbers alone?
error...
The specified number is not valid for this command |
|
|
ZiggyS1 (Programmer) |
21 Jan 11 14:36 |
the mor eI read the more I learn :)
if [[ "$ORDN" != "" ]]
works for string |
|
p5wizard (IS/IT--Management) |
21 Jan 11 14:46 |
> the more I read the more I learn :) Well, that's the idea really HTH,
p5wizard |
|
The Korn Shell book I like best is on Addison Wesley by Anatole Olczak. It presents it in a good order and goes into just enough depth on each topic. I've recommended it to a lot of people and a lot of people swear by it now.
|
|
|
ZiggyS1 (Programmer) |
24 Jan 11 14:05 |
thanks Sam, I saved it to my Blackberry so I don't forget what its called. |
|
|
ZiggyS1 (Programmer) |
28 Jan 11 8:41 |
sorry feherke, I tried to * (Star) your post as well but it only let me do 2... is there a limit? |
|
|
feherke (Programmer) |
28 Jan 11 9:12 |
Hi Quote (Ziggy):sorry feherke, I tried to * (Star) your post as well but it only let me do 2... is there a limit?
As far as I know the star has to be unique per giver & receiver & thread. But I never read a concise rule regarding that. Anyway, thank you for your kind intention. Feherke. http://free.rootshell.be/~feherke/ |
|
|
ZiggyS1 (Programmer) |
28 Jan 11 9:25 |
np... got your star now :) I have 2 screens and the confirmation popped up out of view ó¿ó |
|
|
ZiggyS1 (Programmer) |
2 Feb 11 10:04 |
just another question, I can't put my finger on this.... sometimes when I first login the menu does not fully load, it will only display the field requiring input. This only seems to happen on the mobile scanner, and after a few scans the screen seems to refresh.
But the confusing thing is it only does it once, if I log out and login again it is fine, so it does not really seem to be the program.
|
|
Hi Personally I feel inclined to blame the terminal and/or tput. Maybe the terminal failes to deliver correct environment information to tput or tput wastes so time with preparing its database for use. Personally I would try it without tput, hoping that ANSI escape sequences will work on your terminal : CODE#!/bin/sh
LOGO='RF Scan Menu' bold='\033[1m' offbold='\033[0m' counter=1 ORDN=' stmp="$( date )" amenu='Type exit to close'
while :; do
clear
printf " \t$bold Company $offbold \t$bold %s $offbold
\txxxxxxxxxxxxxxxxxxxxxx \tx %-18s x \txxxxxxxxxxxxxxxxxxxxxx
\tScan Barcode
" "$LOGO" "$amenu"
if [[ "$ORDN" == ' ]]; then echo -n '\tOrder: ' read ORDN continue fi
echo "\tOrder: $ORDN\n"
echo -n "\tScan ( $(( counter++ )) ) : " read answer
case "$answer" in 'exit') break ;; *) echo "$ORDN,$answer,$stmp" >> '/home/zigsto/ScanTest2.txt' ;; esac
done
clear Tested with mksh. Feherke. http://free.rootshell.be/~feherke/ |
|
|
ZiggyS1 (Programmer) |
7 Feb 11 16:36 |
thanks feherke
I'll have to come back to that because it seems a bit different from what I am working with.
I probably should do new posts, but this is bugging me right now.....
I am using this expression to extract a portion of a string, but it gives me an error if I use letters in the variable
let lblval=$(echo `expr substr $lbl 3 $upc1`)
lblval=RRRR: 0403-009 The specified number is not valid for this command.
|
|
|
p5wizard (IS/IT--Management) |
7 Feb 11 16:48 |
let is for number variables only. just leave it out HTH,
p5wizard |
|
Hi As p5wizard already pointed out the error, I can only criticize your style ( again )  . - no need for expr
- no need for that echo
- no need for 2 child processes
CODElblval="${lbl:2:upc1}" Feherke. http://free.rootshell.be/~feherke/ |
|
thanks p5wizard, that worked. feherke, is your syntax giving me errors because I am using ksh and you are posting sh ? You last example gave me error, I tried exactly as you had it (curly braces). error: CODE0403-011 The specified substitution is not valid for this command. |
|
PHV (MIS) |
8 Feb 11 8:33 |
|
Hi Quote (Ziggy):feherke, is your syntax giving me errors because I am using ksh and you are posting sh ?
It is KSH. ( See man ksh | Parameter Expansion. ) More exactly it is KSH93 syntax. Seems your is just KSH88. CODEmaster # lbl='ZiggyS1'
master # upc1=4
master # lblval="${lbl:2:upc1}"
master # echo "$lblval" ggyS
master # echo "$KSH_VERSION" @(#)MIRBSD KSH R33 2008/04/11 Then you will have to keep using expr, but the echo and one child process can still be removed : CODElblval="$( expr substr "$lbl" 3 "$upc1" )" ( Note that you better quote your variables, because without them expr would fail if $lbl contains IFS characters or is empty. ) Feherke. http://free.rootshell.be/~feherke/ |
|
|
ZiggyS1 (Programmer) |
8 Feb 11 11:01 |
yes our version is 88, thanks for the tips. I am making good progress. I need to go back and clean up my code, there are many things to keep in mind but I have learned a ton since I started this project. |
|
|
ZiggyS1 (Programmer) |
9 Feb 11 10:50 |
feherke had a chance to dissect this now.... This line... CODEecho "\tScan ( $(( counter++ )) ) : " returns this error... CODEcounter ++ : 0403-053 Expression is not complete; more tokens expe cted. I am not clear how it works, I gather it is a "Counter" and should display the quantity scanned. the other thing I am not clear on is how do I position where to "read", with "tput cup" I had cursor positions to work with. is \t a shortcut for print? |
|
|
ZiggyS1 (Programmer) |
16 Feb 11 10:25 |
Thanks PHV, I tried this script (posted by: feherke Feb 11 3:10 ), and it also has issue loading the menu complete on the first pass after logging into shell, I tried with ksh and sh, usually efter exiting the script and going back in the menu fully displays. what is consistent is that the line being "Read" will always display, but things like the captions (except read line) or header do not. *** this does not happen on a PC telnet, but only on eg... first login.... After exiting Script.... CODE Company RF Scan Menu
xxxxxxxxxxxxxxxxxxxxxx x Type exit to close x xxxxxxxxxxxxxxxxxxxxxx
Scan Barcode
Order:
Telnet on Scanner (Symbol MC9090) Wavelink CE Version 6.0.42 if you think this should go to another section in the forum, let me know. thanks |
|
|
 |
|