LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 05-10-2017, 07:47 AM   #1
z_haseeb
Member
 
Registered: Jun 2008
Posts: 137

Rep: Reputation: 1
howto pass argument with a script to execute a command


I have a bash script, name script.sh with below data. I want to ran script.sh start which only triggers(ip route add 10.10.20.1 via 192.168.24.1 dev eth0 src 192.168.24.100) and if I run script.sh stop which only triggers (ip route del 10.10.20.1 via 192.168.24.1 dev eth0 src 192.168.24.100). When I run the below script, it executes but the ip route command cant run. please advice, I am not much good in code.


#!/bin/bash
start ()
{
ip route add 10.10.20.1 via 192.168.24.1 dev eth0 src 192.168.24.100
}
stop ()
{
ip route del 10.10.20.1 via 192.168.24.1 dev eth0 src 192.168.24.100
}
 
Old 05-10-2017, 07:59 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
do you know how to use case in bash?
http://tldp.org/LDP/Bash-Beginners-G...ect_07_03.html
 
Old 05-10-2017, 08:20 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Code:
start () {
 ...
}
stop () {
 ...
}
Basically your script contains two functions. Functions are a group of commands that are not run until "called" by the script and your script as posted never calls start or stop. As suggested you can test your command line argument via a case statement to call your functions.
 
Old 05-10-2017, 08:38 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Functions BASH

Chapter 24. Functions
8. Functions
24.1. Complex Functions and Function Complexities
Case Statements BASH
7.3. Using case statements

Shebang (Unix)

Shebang (Linux)

Last edited by BW-userx; 05-10-2017 at 08:40 AM.
 
Old 05-11-2017, 07:04 AM   #5
z_haseeb
Member
 
Registered: Jun 2008
Posts: 137

Original Poster
Rep: Reputation: 1
Thanks guys. I am able to do it.(I know there might be many mistakes in below script but it worked for me)

#!/bin/bash
DESTINATIONIP=10.10.20.1
GATEWAY=192.168.24.1
SOURCEIP=192.168.24.100
DEVICE=eth0
START="ip route add $DESTINATIONIP via $GATEWAY dev $DEVICE src $SOURCEIP"
STOP="ip route del $DESTINATIONIP via $GATEWAY dev $DEVICE src $SOURCEIP"

#####################################
# DONT CHANGE BELOW #
####################################

case "$1" in
start)
$START
;;
stop)
$STOP
;;
#esac
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling lftp from a bash script; howto pass variables into the command syntax stvy Programming 2 01-09-2013 01:43 PM
[SOLVED] how to pass argument from command prompt smritisingh03 Linux - Newbie 3 01-08-2011 09:18 AM
How To pass argument in Expect command? jprathap Linux - Newbie 3 08-27-2009 04:58 PM
[SOLVED] pass file-name as command line argument vikas027 Programming 2 10-14-2008 11:52 PM
Howto pass an argument to alias of a script? Goose1 Programming 1 02-17-2008 06:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration