i need help to find the error in the unix code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting i need help to find the error in the unix code
# 1  
Old 02-19-2012
i need help to find the error in the unix code

i need help i am facing a problem, with this code and i dont know what is it , i need to check how many parameter the user enter and what was the first parameter if the user enter 4 parameter i have to check if the first parameter equal to 1 or 3 if its not equal i have to exit . and i have to check if the number of parameters equal to 6 i have to check if the first parameter equal to 2 if not i have to exit. finally if the parameters equal to 2 i have to check if the first parameter equal to 4 m if not i have to exit .
Code:
if[$# -eq 4] then
if[$1 -ne 1] then
if[$1 -ne 3] then
echo " number of parameters for the selected update less than or greater from the required number1."
exit
fi
fi
 
 
 
 
elif[$# -eq 2] then
if[$1 -ne 4] then
echo " number of parameters for the selected update less than or greater for the required2 "
exit
fi
elif[$# -eq 6] then
if[$1 -ne 2] then
echo " number of parameters for the selected update less than or greater for the required3 "
exit
fi
else
echo" number of parameters less than or greater than the required4 "
exit
fi


Last edited by Scott; 02-19-2012 at 05:15 AM.. Reason: Please use code tags
# 2  
Old 02-19-2012
Hi, you need to use proper spacing around the square brackets and you need to separate "then" from the test with a semicolon.
Code:
if [ $# -eq 4 ]; then

If you are posting code, it is a good idea / custom here, to use [code]..[/code] tags around it and to use indenting. This will improve readability.
# 3  
Old 02-19-2012
if gave me 'then ' unmatched ..
# 4  
Old 02-19-2012
It is looking for a fi
Code:
if [ $# -eq 0 ]; then 
  echo hello
fi

# 5  
Old 02-20-2012
Read up on the getopts command for processing arguments.

Perhaps it could be of use depending on your requirements for calling the script.

Last edited by gary_w; 02-20-2012 at 11:48 AM..
# 6  
Old 02-20-2012
You should also consider using logical ANDs or ORs in yor code to reduce the number of lines of code and make it more easily understood.

For example here is one way to re-write your first code block
Code:
if [ $# -eq 4 ]; then
   if [ $1 -ne 1 -a $1 -ne 3 ]; then
      echo "Number of parameters for the selected update less than or greater from the required number1."
      exit
   fi
fi

You could also re-write the code block in a more modern, but slightly less portable, syntax
Code:
if [[ $# == 4 ]]; then
   if [[ $1 != 1 && $1 != 3 ]]; then
      echo "Number of parameters for the selected update less than or greater from the required number1."
      exit
   fi
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and delete files and folders which are n days older from one unix server to another unix server

Hi All, Let me know how can i find and delete files from one unix server to another unix server which are 'N' days older. Please note that I need to delete files on remote unix server.So, probably i will need to use sftp, but question is how can i identify files and folders which are 'N'... (2 Replies)
Discussion started by: sachinkl
2 Replies

2. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

3. Shell Programming and Scripting

find error?? find / -name "something.txt" 2>/dev/null

why is this giving me errors? i type this in: find / -name "something.txt" 2>/dev/null i get the following error messages: find: bad option 2 find: path-list predicate-list :confused: (5 Replies)
Discussion started by: magiling
5 Replies

4. Shell Programming and Scripting

error code 137 and error code 35072

Hi while trying to run few scripts /afs/ae.ge.com/informatica/ardw/dev/bin/cdw_ar_update_recvbal.sh this script contains the below data load_dir=/afs/ae.ge.com/informatica/ardw/dev/data prog_dir=/afs/ae.ge.com/informatica/ardw/dev/bin ctl_dir=/afs/ae.ge.com/informatica/ardw/dev/ctl... (1 Reply)
Discussion started by: laxmi131
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX to find the unix mail id

Hi all, Need you assistace. is there any command available in UNIX to find the unix mail id ? tried using 'mail' but no use kindly helpe me out. Thanks in advance. manas (2 Replies)
Discussion started by: manas6
2 Replies

6. Shell Programming and Scripting

Little bit weired : Find files in UNIX w/o using find or where command

Yes , I have to find a file in unix without using any find or where commands.Any pointers for the same would be very helpful as i am beginner in shell scritping and need a solution for the same. Thanks in advance. Regards Jatin Jain (10 Replies)
Discussion started by: jatin.jain
10 Replies

7. HP-UX

URGENT: UNIX FTP cannot find path error

Dear all We are currently working on to install some ERP system in wjhich we need to FTP from unix to windows 2000 machine We run ./lodrun to get files from d/xxxxx/xxxx directory but the ftpoutput.log file shows following error FTP: xxxxxxxxx system cannot find the path specified... (1 Reply)
Discussion started by: minix
1 Replies

8. SCO

Need for UNIX Error code list

Hi Unix, Could you please send me the list of error codes and thier description list to my Email ID. I need it very urgetly. Hence please send me this list asap. I want error code list for SCO UNIX (UnixWare 7.1.0) My Email Id is Cheers, Maharaj. (1 Reply)
Discussion started by: smaha_raja
1 Replies

9. Shell Programming and Scripting

how to find a file in UNIX without find command?

given a start directory,a filename,how to find it? (3 Replies)
Discussion started by: bluo
3 Replies
Login or Register to Ask a Question