Visit Our UNIX and Linux User Community


while and do problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while and do problems
# 1  
Old 12-15-2011
Question while and do problems

Any ideas how to clear this error as it seems I dont understand if,do,while and els commands

Code:
#!/bin/ksh
set -x
print "This script creates test messages"
print "Please enter test case name"
read testcasename
echo $testcasename
 
skipfield=Y
while [ $skipfield != "Y" ]
print "Do you want to skip this field Y/N?"
read skipfield
do
done this done is causing syntax error 
 
CHOICE=N
while [ $CHOICE != "Y" ];
do
print "Please enter in field 1 Version"
read version
if [[ "${#version}" -lt 4 ]] ; then
print -n $version >> $testcasename
else
print "too many bytes"
fi
print "Do you want to continue Y/N?"
read CHOICE
done
CHOICE2=N
while [ $CHOICE2 != "Y" ];
do
print "Pleasent in field 2 Date"
read date
if [[ "${#date}" -lt 9 ]] ; then
print -n $date >> $testcasename
else
print "too many bytes"
fi
print "Do you want to continue Y/N?"
read CHOICE2
done

exit


Last edited by methyl; 12-16-2011 at 11:10 AM.. Reason: please use code tags
# 2  
Old 12-15-2011
Quote:
Originally Posted by andrew.p.mcderm
Any ideas how to clear this error as it seems I dont understand if,do,while and els commands

code #!/bin/ksh
set -x
print "This script creates test messages"
print "Please enter test case name"
read testcasename
echo $testcasename

skipfield=Y
while [ $skipfield != "Y" ]
print "Do you want to skip this field Y/N?"
read skipfield
do
done this done is causing syntax error
two problems in this code fragment.
1. If skipfield != Y and before declare skipfield=Y, then never going to enter to while.
2. Position of do.
# 3  
Old 12-16-2011
So I am doing this backwards?
# 4  
Old 12-16-2011
This has been answered on your previous thread:
https://www.unix.com/shell-programmin...ng-script.html
Also the answer in post #2 shows the correct position of while, do, done.
# 5  
Old 12-16-2011
Code:
# this is forever loop -almost, try to press CTRL-D ... loop will end
skipfield=Y
while [ $skipfield != "Y" ]
print "Do you want to skip this field Y/N?"
read skipfield
do
      :   # between do and done, like then and fi need 1-n commands
         #  :  = builtin nop command, return always true
done

Why ?
while command(s)
do
...
done
=> exit code of command, between while and do you can put command lines, but return code of last line is the important.
=> read some and press EOF = exit code is not 0 => while end

So, maybe this is better:
Code:
# this will end if you press Y or CTRL-D
while
     print "Do you want to skip this field Y/N?"
     read skipfield
     [ "$skipfield" != "Y" ]  # - without " " you get error if press ENTER or CTRL-D
do
      : 
done


Last edited by kshji; 12-16-2011 at 11:38 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #539
Difficulty: Medium
In C, int x; creates an integer variable associated with the identifier x initialized to one .
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

About ps -ef and if-else problems

I'm new in Shell Programming and Scripting, I would like to ask some questions. ps -ef | grep $appNAme | grep -v grep <-- what will it return when it find a process is running? return 1 or 0 if then exit 1 <--- if = 0 , run this ? else continue <--- if = 1 , run this ?... (5 Replies)
Discussion started by: LoAlex
5 Replies

2. UNIX for Dummies Questions & Answers

Problems with IF Else ??!!

I have written the following code in cygwin to create directories in windows based on the parameter passed. echo $1 > fullpath path1=`awk -F / '{print $1}' $fullpath path2=`awk -F / '{print $2}' $fullpath if then if then if then continue ... (4 Replies)
Discussion started by: janardhanamk
4 Replies

3. SuSE

Problems!!

good friends I am new to linux and I have the following TELNET service problem entering data devo telnet to a Windows server 2003 server and passes will not let me since the move to this server by telnet tells me the characters and thus invalidates me income, I have a service application... (1 Reply)
Discussion started by: pepetico
1 Replies

4. Shell Programming and Scripting

Problems with $?

Hello, I have the following piece of code that tries to retrieve the result of a .sh: . $HOME/prueba/Scripts/Recogida/recogida_sedra.sh resultado=$? echo "el resultado es : $resultado" if ; then echo "Se va a preprocesar los archivos" In the code of recogida.sh I have the... (8 Replies)
Discussion started by: danietepa
8 Replies

5. UNIX for Dummies Questions & Answers

Problems with using less

Hello, I am having problems with using less on Linux version 2.6.18-92.1.17.el5 (brewbuilder@hs20-bc1-7.build.redhat.com) (gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)). I am using csh but have the same problems on bash. If I pipe something to less it works perfectly i.e. cat file | less... (9 Replies)
Discussion started by: z1dane
9 Replies

6. UNIX for Dummies Questions & Answers

problems with If

I'm having problems uses "if" it works fine when i do this. #!/bin/sh a= 10 qw= 2 w= 20 { if && ;then echo 3 fi } However if i try to do #!/bin/sh a= 10 (5 Replies)
Discussion started by: THM
5 Replies

7. UNIX for Advanced & Expert Users

Problems with Last

Hi, I,ve a Unixware 7.1.3 working correctly for two years ago, since a several weeks I've a problem with the command last . The information that this command return is : For example : 1.- The user root , time login : 12:15 h, time logoff 12:15 h (the real time is 14:00). Connected time is... (3 Replies)
Discussion started by: By_Jam
3 Replies

8. UNIX for Dummies Questions & Answers

Few problems

Hi how can i do this? 1) shell script which writes data and time on to a file if filesystem exceeds 70% of space. 2) make entry to cron table to run a script every 15 mins. and can anyone expplain or demonstrate the difference between variables used in inside a function and outside a... (3 Replies)
Discussion started by: vivekshankar
3 Replies

9. UNIX for Advanced & Expert Users

'make' problems (compliation problems?)

I'm trying to compile and install both most recent version of 'make' and the most recent version of 'openssh' on my Sparc20. I've run into the following problems... and I don't know what they mean. Can someone please help me resolve these issues? I'm using the 'make' version that was... (5 Replies)
Discussion started by: xyyz
5 Replies

Featured Tech Videos