KSH Script Assistance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH Script Assistance
# 1  
Old 05-07-2007
KSH Script Assistance

Hey everyone, I'm newer than new when it comes to this ksh and scripting stuff, and unix in general. I have been thrown into a task at work that
A: They expect me to come up to speed on,
B: Show I can do this. (Program for the workgroup)

Here's the script, part of it, from the overall script:

if [ $# -eq 1 ]; then
echo "the files that exist...... ...."
FILEDIR="$1"
elif [ $# -eq 2 ]; then
echo "The files exist in $1/$2..... .... "
FILEDIR="$1"
SUBDIR="$2"
else
echo "Script help text"
echo "script filedir [subdir]
echo " "
exit 1
fi.

OK, here's what I know/understand:
I can type the script name, followed by one or 2 paramaters that it will see, and work on. Or, if no parameters are entered, we get that help screen. So far, so .. so.
I also see where $1 and $2 are assigned to parameters or variables, depending on when they are typed.

My task (At the moment) is to add a '-h' option to bring up the help text rather than let it run when no parameters are supplied. I don't know or have figured out how, despite reading through a few books today because I'm not sure what I am looking for precisely.

My first issue is I do not understand the '$#' or the '-eq'.
My second issue is how deep can if's be nested? (If that's where I need to go with this one.)

Any help would be appreciated. AND If anyone can point me to good resources on the web to review or learn from for future reference? I need to learn what I'm looking at so I don't have to ask help for what seems to be simple questions.

Thanks!

Last edited by Brusimm; 05-09-2007 at 08:54 AM..
# 2  
Old 05-08-2007
-eq means equal (integer), -lt less than, -gt greater than, etc.
You can nest if statements.

$0, $1, $2,...$9, $#, and $* are defined by the shell itself. $# is the number of parms.

What you probably want is getopts. It is used for things like -h.
# 3  
Old 05-10-2007
Quote:
Originally Posted by jim mcnamara
-eq means equal (integer), ......
What you probably want is getopts. It is used for things like -h.
Thanks Jim....

Would I use the getopts in place of the structure I have here, or add in after the code I've got?

Last edited by Brusimm; 05-10-2007 at 02:43 AM..
# 4  
Old 05-10-2007
Quote:
Originally Posted by Brusimm
My second issue is how deep can if's be nested?
There is no arbitrary limit. But at 10 to 15 the code would hard for a human to read so you don't want to over-do it. Search for getopts on this site and you will find plenty of scripts that use it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need assistance in ksh script

requirement : I need to read a text file and find out which particular line has highest charcters on it using the shell script. I tried & was able to find out only for one line. I could not able to find out for the entire the line. sed -n '10 p' ctstest.sh | wc -w Please guide me... (5 Replies)
Discussion started by: ramkumar15
5 Replies

2. Shell Programming and Scripting

Dummy script assistance

Hi, I am new in ksh scripting and if anyone can help that would be great. I'm writing a script which will SSH to several machines and then would append a certain file from a NAS to the /etc/sudoers file the problem i am having is after the script connects to a certain server the commands are... (7 Replies)
Discussion started by: galuzan
7 Replies

3. Shell Programming and Scripting

Need assistance with a simple script

I have a simple script. Do you know what I got this error? ./total_memory.ksh: line 5: ' Thanks #! /bin/bash setmem=30177660 totalMemory= grep MemTotal /proc/meminfo | awk '{print $2}' if ; then echo "Total memory $totalMemory is less than :$setmem" exit 1 ... (3 Replies)
Discussion started by: Beginer0705
3 Replies

4. Shell Programming and Scripting

Need assistance to resolve the KSH issue

am running the small script below. count_a=48 count_b=48 if ; then echo "Count matched" else echo "count not matched" fi I got the below output. /bin/ksh: [48: not found count not matched It was giving the same error when I ran in another box. But I inculded /bin/ksh in the... (10 Replies)
Discussion started by: sathik
10 Replies

5. Shell Programming and Scripting

script assistance with shift J

Hey all, I need some assistance. I'm writing a script to eject tapes from a tape library, but the library is not a queued system and can only eject 15 tapes at a time. I added paste -d : -s so that it goes through full_tapes and puts each media_id on one line separated by the :. Now I'm... (2 Replies)
Discussion started by: em23
2 Replies

6. Shell Programming and Scripting

Shell Script Assistance

I am looking for a shell script or command to automate a process of opening many files in a directory and changing a string of text. Example: I have a apache web server that uses virtual hosting. There are approximately 2300 vhost entries or files. So in the directory... (2 Replies)
Discussion started by: jaysunn
2 Replies

7. Shell Programming and Scripting

shell script assistance please

When I run this command (showstatus <username> <dbname>) in the prompt, the following will be displayed in the screen: 1. Show processes 2. Start process 3. Stop process 4. Go back to prompt Once i choose/type Option "1" (which is Show processes), it will display the list of processes... (5 Replies)
Discussion started by: xinoo
5 Replies

8. 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

9. Shell Programming and Scripting

need assistance ----SH schell script

Hello All, I need to develop a script(SH]) to generate a comparison file between two files old and new file.The script takes in parameter the old file path and the new file path. And the script generates a file containing the comparison between the two files with this details: - Keys... (2 Replies)
Discussion started by: shahidbakshi
2 Replies

10. Shell Programming and Scripting

Need a little assistance with a shell script

I need to modify a script to send an attatched file. I have researched and read the faq's but have not found a solution for my script. Here is a copy of the code I am using: #!/bin/sh mysqldump --opt --skip-add-locks --user=****** --password=******* databasename | gzip >... (3 Replies)
Discussion started by: rickou812
3 Replies
Login or Register to Ask a Question