Find the answer to your Linux question:
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...
  1. #1
    Just 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

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Yes.


    (I am not going to mention them by name, because this could mislead you to the wrong assumption that this list was complete. )

  3. #3
    Linux 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

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    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
    You shouldn't use "source" unless you are doing it to get the variable definitions. Otherwise, use one of the other methods.


    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

  5. #5
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Let's not forget exec.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...