Troubles with shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Troubles with shell script
# 1  
Old 04-04-2011
Troubles with shell script

Hello. I'm having trouble figuring out how to write this script. I'm supposed to have my script start up with specific arguments and if there are missing arguments or invalid arguments, return an error associated to its errorlevel number.
For example, here are a few errorlevel numbers with their event information.

16 Third argument present, but not what expected (“-o”)
17 Fourth argument missing on command line
18 Too many arguments provided on command line (>4)

First off, i'm supposed to execute my shell script with these specific arguments :

Code:
Testscript.sh -i ./input.txt -o ./output.txt

I've added the following to my script to make sure the script is started with those arguments :

Code:
$# = 4

$0 = ./testscript.sh

$1 = "-i"

$2 = "./input.txt"

$3 = "-o"

$4 = "./output.txt"

$@ = " -i ./input.txt -o ./output.txt"

The part i can't seem to figure out is how to get my script to return an error and an errorlevel number. I was thinking maybe using IF statements in conjungtion with the $? variable but i'm not totally sure. Help would be much appreciated.

Thanks.
# 2  
Old 04-04-2011
Code:
if [ $# -lt 4 ]
then
    echo 'Forth argument missing on command line'
    exit 17
fi
 
if [ $# -gt 4 ]
then
    echo 'Too many arguments provided on command line (>4)'
    exit 18
fi
 
if [ "$3" != "-0" ]
then
    echo 'Third argument present, but not what expected ("-o")'
    exit 16
fi

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 04-04-2011
Wow thank you very much. This makes much more sense now. Heh, simple answer but i'm learning.
# 4  
Old 04-04-2011
When printing error messages, you usually want to redirect them to stderr instead of using stdout.
Code:
echo error message 1>&2

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 04-04-2011
Thanks Alister, I originally had the echo statements as comments as the spec didn't seem to call for any error output.

However I think the above is much more usefull, self-documenting and educational for a beginner scripter than:

Code:
[ $# -lt 4 ] && exit 17
[ $# -gt 4 ] && exit 18
[ "$3" != "-0" ] && exit 16

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Have troubles with bash script: xubuntu systemd.link onboard

Hey there. I'm new in write bash scripts in fact this is my first one so please be patient ;). Also english is not my native language but i hope you understand me anyway. I installed xubuntu on my mothers laptop and every time a new version update gets installed the keyboard doesn't work... (9 Replies)
Discussion started by: Apop85
9 Replies

2. UNIX for Beginners Questions & Answers

Troubles running DB2 command in shell script via cron

Hi there, Now I'm facing error regarding running shell script via cron. The shell script which is required to get value from database. Below is the main part of shell script. #/bin/bash #connect to database(1) db2 connect to $database user xxxx using yyyy #set values from... (3 Replies)
Discussion started by: Rohan Kishibe
3 Replies

3. UNIX for Advanced & Expert Users

Troubles with OpenSSH

Hi, I am trying to login from one AIX server to another without using a password, a basic configuration, however it doesn't seem to work. All things are in place. I have both a public and private key in the ~/.ssh folder and also have an "authorized_keys" file on the target-server containing... (5 Replies)
Discussion started by: Hille
5 Replies

4. BSD

PF troubles on OpenBSD 5.0

I am setting up a system as an ADSL gateway. ADSL is working fine. PF is not forwarding for some reason. # ifconfig lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33196 priority: 0 groups: lo inet6... (0 Replies)
Discussion started by: John Tate
0 Replies

5. UNIX for Dummies Questions & Answers

Cron troubles

I am aware this question has been answered time and again. I feel I have tried everything I have seen on the net and really need help to get this working. Same old story. Shell script, working from command but not from cron. I need my script to take values from a .properties file. Tried... (2 Replies)
Discussion started by: airalpha
2 Replies

6. HP-UX

cron troubles

I have a cronjob that I need to run everyday and it needs to have todays date inputed, here is what I have, but is not working as expected.......... 23 02 * * * cd /path;./RequestSummaryReport.sh $(date +%Y-%m-%d) the output from mail gives me............. Date: Fri, 8 Feb 2008 02:12:07... (4 Replies)
Discussion started by: theninja
4 Replies

7. Programming

Troubles with HPUX

Hello I created an application in c language for HP-UX operative system,and it runs on a 32 bits PARISC processor. My problem is that I have to run this same application but now in a 64 bits Parisc processor. But I am not able to compile the application with the 64 bit server, and I only could use... (1 Reply)
Discussion started by: masterboy6666
1 Replies

8. UNIX for Dummies Questions & Answers

compariosn troubles...

Hi Guys, I am trying to compare using if, but keep getting some strange results. if ; then keeps creating the file 1 if ; then does not work at all if ; then does not work if ; then does not work if ; then does not work eihter. I am using a ksh, on Solaris (9 Replies)
Discussion started by: jagannatha
9 Replies

9. UNIX Desktop Questions & Answers

NetBSD Troubles

HI, I just installed NetBSD i386 v. latest. I am having a few problems: For some reason X just won't start!? Another is I can't access My CDROM or Floppy. Thanks in advance! (3 Replies)
Discussion started by: vertigo
3 Replies

10. Programming

compiling troubles

i keep getting the following error with the code segment below when i try to compile the program. The code is from 'defs.h' parse error before '(' parse error before ')' stray '\' in program this is the code segment and the error is on the second line of the segment #define... (1 Reply)
Discussion started by: token
1 Replies
Login or Register to Ask a Question