Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with controlling loop in script and debugging 5

Status
Not open for further replies.

ZiggyS1

Programmer
Oct 6, 2008
97
CA
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
 
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...
Code:
if ( $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).

 
thanks

ok I decided to experiment in this area with the if....

Code:
if ( $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.
 
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
 
I figured it out. I will post as soon as I clean it up.
 
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
 
Hi

Just to clean up a mistake.
SamBones said:
[tt]if ( $counter > 1 )[/tt]
Ziggy said:
[tt]if ( $ORDN = "" )[/tt]
Syntactically correct, but probably not what you want.
man ksh said:
((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 [tt](( ))[/tt] is for numeric evaluation only and if I understand correctly $ORDN may contain any string, better use [tt][[ ]][/tt] for the test :
Code:
[b]if[/b] [teal][[[/teal] [green][i]"$ORDN"[/i][/green] [teal]=[/teal] [green][i]""[/i][/green] [teal]]][/teal]
[b]then[/b]
  tput cup  [purple]8[/purple] [purple]3[/purple][teal];[/teal] echo [green][i]"Order: "[/i][/green]
  tput cup [purple]8[/purple] [purple]10[/purple][teal];[/teal] [COLOR=chocolate]read[/color] ORDN
[b]fi[/b]


Feherke.
 
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...
Code:
#!/bin/sh
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...
Code:
#!/bin/ksh


 
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




 
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
 
the mor eI read the more I learn :)

if [[ "$ORDN" != "" ]]


works for string
 
> 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.


 
thanks Sam, I saved it to my Blackberry so I don't forget what its called.
 
sorry feherke, I tried to * (Star) your post as well but it only let me do 2... is there a limit?
 
Hi

Ziggy said:
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top