need help with ksh script errors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help with ksh script errors
# 1  
Old 02-21-2012
need help with ksh script errors

Code:
 
#!/bin/ksh
number1="20"
if [ $1 ]
then
        number1=$1
fi
number2="1"
while [[ $number2 -lt $number1 -o $number2 -eq $number1 ]]
do
        if [[ let "number2 % 3" -eq 0 -a let "number2 % 5" -eq 0 ]]
        then
                print "FizzBuzz"
        elif [[ let "number2 % 3" -eq 0 ]]
                then
                        print "Fizz"
                elif [[ let "number2 % 5" -eq 0 ]]
                        then
                                print "Buzz"
                        else
                                print $number2
        fi
        let "number2++"
done

I am very new to scripting so sorry if this is really messed up. The intention is to use the command line to get a number from the user and then print the numbers 1 through n. only if n is divisble by 3, 5, or both it prints fizz buzz and fizzbuzz instead. also if the command is not enetered the number is set to 20.
With this code i get the error [[: not found and if i only use single "[";s i get test: number2 % 3 unkown operator. please help ive been stuck on this for a while now. thank you. (do i need to set the PATH variable to something specific?) Also this is being done in korn shell
# 2  
Old 02-21-2012
Code:
number1="20"
if [ -n "$1" ]
then
    number1=$1
fi

number2=1
while [[ $number2 -le $number1 ]]
do
    if [[ "$number2 % 3" -eq 0 && "$number2 % 5" -eq 0 ]]
    then
        echo "FizzBuzz"
    elif [[ "$number2 % 3" -eq 0 ]]
    then
        echo "Fizz"
    elif [[ "$number2 % 5" -eq 0 ]]
    then
        echo "Buzz"
    else
        echo "$number2"
    fi
    number2=$(($number2 + 1))
done

# 3  
Old 02-21-2012
You can try this also. This should work.!
Code:
#!/bin/ksh
number1="20"
if [ ${1} ]
then
        number1=${1}
fi
number2="1"
while [[ $number2 -le $number1 ]]
do    
  if [[ $(($number2 % 3 )) -eq "0" && $(($number2 % 5)) -eq "0" ]]; then
    echo "FizzBuzz"
  elif [[ $(($number2 % 3)) -eq "0" ]]
  then
    echo "Fizz"
  elif [[ $(($number2 % 5)) -eq "0" ]]
  then
    echo "Buzz"
  else
    echo "$number2"
  fi
  number2=$(($number2 + 1))
done


Last edited by Franklin52; 02-21-2012 at 07:32 AM.. Reason: Please use code tags for code and indent your code, thank you
# 4  
Old 02-21-2012
Using let:
Code:
#!/bin/ksh
number=${1:-20}
for (( i=1; i<=$number; i++ )); do
  if   ! let "i % 3 || i % 5 "; then 
    echo "$i FizzBuzz"
  elif ! let "i % 3"; then
    echo "$i Fizz"  
  elif ! let "i % 5"; then
    echo "$i Buzz"  
  fi  
done

or the (( )) version:
Code:
#!/bin/ksh
number=${1:-20}
for (( i=1;i<=$number;i++ )); do
  if   ! (( i % 3 || i % 5 )); then 
    echo "$i FizzBuzz"
  elif ! (( i % 3 )); then
    echo "$i Fizz"  
  elif ! (( i % 5 )); then
    echo "$i Buzz"  
  fi  
done


Posix:

Code:
number=${1:-20}
i=1
while [ $i -le $number ]; do
  if   [ $(( i % 3 )) -eq 0 ] && [ $(( i % 5 )) -eq 0 ]; then
    echo "$i FizzBuzz"
  elif [ $(( i % 3 )) -eq 0 ]; then
    echo "$i Fizz"  
  elif [ $(( i % 5 )) -eq 0 ]; then
    echo "$i Buzz"  
  fi  
  i=$((i+1))
done


Last edited by Scrutinizer; 02-21-2012 at 07:08 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

2. Shell Programming and Scripting

Help with shell script errors

hey watsup guys i am new in the shell script world. so i need help fom you guys, i have written these two codes and they both give the same errors( expr : syntax error). Code 1 : #! /bin/sh # count1 appends an increment to a file 200 times # note that a file called numbers must be... (5 Replies)
Discussion started by: surubi_abada
5 Replies

3. Shell Programming and Scripting

Ksh errors while copying a directory

Hello all, I have a ksh script run on Solaris 10 by the root user. The section that's giving me errors is the following: VAR=$(perl -lne '...') if ] ; then print "$VAR undefined" fi recover_files { print "Recovering the files..." cd ${VAR}/files cp -pr nfs /destination print... (1 Reply)
Discussion started by: AdrianM
1 Replies

4. Shell Programming and Scripting

Script to capture errors

Hello; I'm trying to write a script to capture any hardware error from logs/syslog on my SUSE 10 servers so i can be notified if we have any hardware issues such a bad fan or battery, etc.. Thanks in advance for any help (2 Replies)
Discussion started by: Katkota
2 Replies

5. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

6. Shell Programming and Scripting

tracing a ksh script within a ksh script

I normally trace a script with the ksh -x <script name> and redirect strderr to file. But if you have a script like the examble below...... vi hairy bear=`grep bear animals` if then ksh more_animals fi If I ksh -x hairy it won't trace "more_animals" unless I put a -x in it. Is... (1 Reply)
Discussion started by: shorty
1 Replies

7. Shell Programming and Scripting

executing a ksh script from another ksh script

Hi, I'm new to unix scripting.How can i call a script from another script. I have a.ksh and b.ksh .I have to call b.ksh from a.ksh after it is successfully exceuted. I tried using #!/bin/ksh -x in a.ksh and at the end i have used /path/b.ksh My problem is it is executing only a.ksh.it... (6 Replies)
Discussion started by: ammu
6 Replies

8. Programming

Errors while Compiling a PC script.

Hi all, I have created a post-C (PC) script OrdItmpopulate.pc. When I am compiling this using the “Make” command I am getting the following error. My “make” command looks like this: make -f $ORACLE_HOME/precomp/demo/proc/demo_proc.mk build EXE=OrdItmpopulate8.exe OBJS="OrdItmpopulate8.o"... (1 Reply)
Discussion started by: musavir19
1 Replies

9. UNIX for Dummies Questions & Answers

unix script errors

#!/bin/ksh sqlplus -s user/passwd@remoteserver << EOF > errors2.log set pagesize 0 feedback off verify off heading off echo off select directory_nm || '/' || file_nm || '|' ||venue_id || '|' || time_id from file_control; EOF The output of this script is; ... (2 Replies)
Discussion started by: pavan_test
2 Replies

10. Shell Programming and Scripting

checking for script errors

ok, i have a script which i use to search my process' for specific keywords and kill any process containing them. there is a prompt to enter a keyword for searching and another prompt for which user you want to search the process' of. i want the script to have something that if you entered a search... (1 Reply)
Discussion started by: Blip
1 Replies
Login or Register to Ask a Question