Need help to make a shell script with usage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to make a shell script with usage
# 8  
Old 12-13-2012
Thanks Corona, but I know to do this part.

I just need advices to howto write my functions.

Regards
# 9  
Old 12-13-2012
We can't write the whole thing for you.

What have you tried?
# 10  
Old 12-13-2012
This is how my script begin. For the exemple and the exercice, I am trying to execute something with differents programs. As you see, I think something missing. Do I have to write a loop with a for i in ?

Code:
#!/bin/sh
PROGS='ntp apache mysql'

set -e

if [ "$#" -eq 0 ]
then
        echo "Usage: test.sh {install-i prog|remove-r prog}"
        exit 1
fi

install() {
     echo "so you want to install $PROGS? (y/n)"

     read reponse

     if [[ $reponse == "y" ]]
       then apt-get install $PROGS
     fi
}


Last edited by Corona688; 12-13-2012 at 04:53 PM..
# 11  
Old 12-13-2012
Without knowing what you want to do, it's difficult to say how you should write your for-loop.

You can use $# to tell how many arguments are left, and shift to delete the first argument, so you can make a loop while $# is greater than zero and check the value of $1 every loop to determine what to do.
# 12  
Old 12-13-2012
I want to install a program (ntp, apache or mysql) with a shell script. It is just an exercice for me, because I can install it without writing anything and just launch an apt-get install.

I want this usage:
Code:
./myscript.sh -install package name

or
Code:
./myscript.sh -remove package name

where package name are a variable located in my script.

I am sorry if it sounds confused, I cannot speack english very well and I am a beginner in shell programming.

Last edited by Franklin52; 12-14-2012 at 03:33 AM.. Reason: Please use code tags for data and code samples
# 13  
Old 12-14-2012
Hello Francesco

You can use something like this.

Code:
cat test.sh

Code:
#!/bin/sh
while getopts ":n:a:m:" opt; do
  case $opt in
    n)
      installing command for ntp
      ;;
    a)
      installing command for apache
      ;;
    m)
      installing command for mysql
     ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

How to run the script

Code:
./test.sh -a apache , ./test.sh -n ntp , ./test.sh -m mysql

This is the case if you want to remove or install the package one by one for ntp,apache or mysql..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell script that monitors CPU Usage

I'm a newbie to shell scripting, I was given this script to modify. This script that monitors when CPU Usage is too high based off the top command. The comparison is not working as it should. Its comparing a decimal to a regualar interger. When it send me an email, it send an email and ignores the... (21 Replies)
Discussion started by: mhannor
21 Replies

2. UNIX for Dummies Questions & Answers

Basic sed usage in shell script

Hi, A basic sed question. I have a set of files. In each file there is a number that I want replaced. For example, if I run sed I should get the following: % cat test2.txt #goofy//171.00 goofy 171.00 % sed -i 's/171/xxx/g' test2.txt % cat test2.txt #goofy//xxx.00 goofy xxx.00 ... (2 Replies)
Discussion started by: pc2001
2 Replies

3. Shell Programming and Scripting

Shell script to calculate the max cpu usage from the main script

Hi All, I have a script which does report the cpu usuage, there are few output parameter/fields displayed from the script. My problem is I have monitor the output and decide which cpu number (column 2) has maximum value (column 6). Since the output is displayed/updated every seconds, it's very... (1 Reply)
Discussion started by: Optimus81
1 Replies

4. Shell Programming and Scripting

Pattern extraction and usage by shell script

Suppose im in a directory A. which has sub-directories x/y/z m/n/p etc. Iam only considered with those which have a file netl.oa at the lowermost level. So i used the find command which gives me a list in the form ./abc/def/ghi/jkl/netl.oa and so on Now i want the names abc def jkl and ghi. My... (3 Replies)
Discussion started by: sid.verycool
3 Replies

5. Shell Programming and Scripting

Need help on ssh usage in a loop of shell script

I need help on how to connect remote systems through ssh command in while loop of shell script. I was able to connect one remote system using ssh from shell script. Please find sample code snippet as given below….. ssh "root@148.147.179.100" ARG1=$rpmFileName 'bash -s' <<'ENDSSH' ... (5 Replies)
Discussion started by: rajesh.tulluri
5 Replies

6. Shell Programming and Scripting

argv usage in a korn shell script

I'm trying to count and display the variables on a command line #!/bin/ksh $# argv from command line: tryit2 this is a test returns : tryit2: 4: not found. (1 Reply)
Discussion started by: Bperl1967
1 Replies

7. Shell Programming and Scripting

usage of telnet in shell script

Hi All, How could I use telnet to connect to Primary Host in the below shell script: Could I use: ( echo user sleep 2 echo password sleep 2 echo "ls ~" sleep 2 echo "exit" ) | telnet hostname ... (1 Reply)
Discussion started by: a1_win
1 Replies

8. Shell Programming and Scripting

Help need to make a shell script run for ffmpeg vhook watermaking in shell

i have a small problem getting a batxh shell script to run in shell this is the code the problem seems to be centered around the ffmpeg command, something maybe to do with the ' ' wrapping around the vhook part command this is a strange problem , if i take the ffmpeg command and... (1 Reply)
Discussion started by: wingchun22
1 Replies

9. Shell Programming and Scripting

how do i get current bandwidth usage via shell script?

hello unix-people. can u please tell me how i can get the current bandwidth usage of my machine on shell into variables? thanks a lot (2 Replies)
Discussion started by: scarfake
2 Replies

10. UNIX for Dummies Questions & Answers

shell script extended usage...

Hi all, In a shell script, how can I do the following: Find out the size of a mounted usb stick (df -h /media/usbdisk) and use the result (physical size of USB stick) to set a loop-counter according to the disk size?? As a result, I do copy files of 4MB each as many times as they will fit... (1 Reply)
Discussion started by: joerg535
1 Replies
Login or Register to Ask a Question