Results 1 to 5 of 5
Hi All,
Is there any other way of executing shell script??? other than below ones.
1) sh file.sh
2) source file.sh
3) .(dot) file.sh
4) ./file.sh...
- 08-20-2008 #1Just Joined!
- Join Date
- Aug 2008
- Posts
- 49
The other way of executing shell script???
Hi All,
Is there any other way of executing shell script??? other than below ones.
1) sh file.sh
2) source file.sh
3) .(dot) file.sh
4) ./file.sh
- 08-20-2008 #2
Yes.
(I am not going to mention them by name, because this could mislead you to the wrong assumption that this list was complete.
)
- 08-21-2008 #3Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
5) piping
I'll give you a detail example for this:
I have a iptables script in /usr/bin as follows
/usr/bin/firewall-script
#!/bin/bash
...
iptables -t nat -A POSTROUTING -o eth1 -s 192.168.3.0/24 -j MASQUERADE
iptables -t nat -A POSTROUTING -o eth1 -s 192.168.10.0/24 -j MASQUERADE
...
for some reason I need to temporarily change eth1 to tun1 in the above script, the way to do that without modifying that script is to simply run
Code:sed -e '/MASQUERADE/ s/eth1/tun1/g' /usr/bin/firewall-script | bash
- 08-21-2008 #4
For what it's worth, 2 and 3 in your list are the same thing. The dot is just a shortcut for "source".
Also note that sourcing a script is very different from running it normally. A sourced script exports all of its variable definitions to its parent:
You shouldn't use "source" unless you are doing it to get the variable definitions. Otherwise, use one of the other methods.Code:tanya:bash alex$ cat sample_script #!/bin/bash var=Hello echo "My var is $var" tanya:bash alex$ ./sample_script My var is Hello tanya:bash alex$ echo $var tanya:bash alex$ source sample_script My var is Hello tanya:bash alex$ echo $var Hello
Also, 4 on your list is incomplete. What #4 actually is that you can give the full path of a script and it will execute that script. If the script is in the current directory, you can say "./SCRIPT", because "." means "the current directory". However, I could also say "/home/user/script" (absolute path: it starts with "/") or, "test/bash/script" (relative path from my current location).DISTRO=Arch
Registered Linux User #388732
- 08-21-2008 #5Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Let's not forget exec.


Reply With Quote