Find the answer to your Linux question:
Results 1 to 5 of 5
Hi I performed following 3 steps Setting Up Ethernet Card, Install libpcap, Install libdnet...Now i want to automate this using shell scripting. Can anyone explain how to convert the following ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    2

    Linux Script for installation


    Hi

    I performed following 3 steps Setting Up Ethernet Card, Install libpcap, Install libdnet...Now i want to automate this using shell scripting.
    Can anyone explain how to convert the following procedures into a shell script.

    Regards

    Steps are given below...

    -Setting Up Ethernet Card

    nano /etc/network/interfaces

    add following;
    auto eth1
    iface eth1 inet dhcp

    auto eth0
    iface eth0 inet static
    address 10.10.0.1
    netmask 255.0.0.0
    network 10.0.0.0
    broadcast 10.255.255.255


    /etc/init.d/networking restart

    -libpcap
    cd /usr/src
    wget xxx.com
    tar -zxf libpcap-1.2.0.tar.gz && cd libpcap-1.2.0
    ./configure --prefix=/usr --enable-shared
    make && make install


    -Install libdnet:
    cd /usr/src
    wget xxx.com
    tar -zxf libdnet-1.12.tgz && cd libdnet-1.12
    ./configure --prefix=/usr --enable-shared
    make && make install

  2. #2
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Well, you basically just need to write these commands into a file using Nano and run it with your shell interpreter. So lets say your script file is called "/root/my-setup.sh". Run the script with the command bash /root/my-setup.sh.

    Obviously, the parts where you use "Nano", or any interactive tool, will need to be changed. You could just create a template "/root/interfaces.temp" file and copy it right into "/etc/network/" with cp -f /root/interfaces.temp /etc/network/interfaces Or, assuming you are using Bash as your shell interpreter, you can use a feature of the Bash scripting language called a "Here Document." Write something like this into your script directly:
    Code:
    #Overwrite /etc/network/interfaces with my settings...
    cat >/etc/network/interfaces <<-__MY_INTERFACES__
    auto eth1
    iface eth1 inet dhcp
    
    auto eth0
    iface eth0 inet static
    address 10.10.0.1
    netmask 255.0.0.0
    network 10.0.0.0
    broadcast 10.255.255.255
    __MY_INTERFACES__
    If your script needs to modify "/etc/network/interfaces", keeping previous entries untouched and you cannot just overwrite it, then this may be a much more complicated problem and I would avoid using a script unless you know what you are doing.
    Last edited by ramin.honary; 08-07-2012 at 05:27 AM.

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    2

    Usage of sed & su through shell script

    Hi
    Thank you for the reply;
    there are 3 other things in which I need help.

    1: How to replace the following line in file "/etc/apt/sources.list" with my own using sed;

    New Content:
    ------------
    deb xyz.com squeeze main contrib non-free

    Prev content:
    ------------

    deb abc.com squeeze/updates main




    2:Now i tried the following script to check if user is logged in as a root user or not, if not then login as root user but when I run this script the shell get login as root user but does not run the remaining script.

    if [ `whoami` != root ]; then
    echo "$0:Logging as root ";
    su
    fi

    //Remaining script

    3: Can any one share link to some recommended shell script tutorial

    regards

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by xsBravo View Post
    1: How to replace the following line in file "/etc/apt/sources.list" with my own using sed;

    New Content:
    ------------
    deb xyz.com squeeze main contrib non-free

    Prev content:
    ------------

    deb abc.com squeeze/updates main
    Code:
    sed -i.bak 's|^deb abc.com squeeze/updates main|deb xyz.com squeeze main contrib non-free|' sources.list
    2:Now i tried the following script to check if user is logged in as a root user or not, if not then login as root user but when I run this script the shell get login as root user but does not run the remaining script.

    if [ `whoami` != root ]; then
    echo "$0:Logging as root ";
    su
    fi

    //Remaining script
    you can't easily do it that way - it is possible, but a little tricky. it would probably be better to have a wrapper script that checks id and then runs the main script. something like:
    Code:
    #!/bin/bash
    if [ `id -ru` -ne 0 ]; then
      su - c '/path/to/your/script.sh arg1 arg2'
    else
      /path/to/your/script.sh arg1 arg2
    fi

  6. #5
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Quote Originally Posted by xsBravo View Post
    2:Now i tried the following script to check if user is logged in as a root user or not, if not then login as root user but when I run this script the shell get login as root user but does not run the remaining script.
    Code:
    if [ `whoami` != root ]; then
    echo "$0:Logging as root ";
    su
    fi
    Remember, you cannot use interactive programs in a script, and "su" is an interactive program. However "su" and "sudo" can both be used in a non-interactive way for scripts.

    You can execute another script from inside a script. One technique can be to make the script try to execute itself inside of "su".
    Code:
    #!/bin/bash
    if [ `whoami` != 'root' ];
    then  echo "$0: Logging in as root...";
          exec su -c "$0" root -- "$@";
    fi
    The "-c" option lets you specify to "su" that it should run this script. "root" specifies the user you wish to switch to, and the -- "$@" copies the arguments passed to the script to "su" which will copy them to your script when it is run as root. The exec before "su" is important because it ensures the remainder of the script is not run. Otherwise, if "su" failed to authenticate, it will continue to execute the remainder of the script.
    Quote Originally Posted by xsBravo View Post
    3: Can any one share link to some recommended shell script tutorial
    My favorite is Bash Programming Introduction HOWTO and also the Advanced Bash Scripting Guide.

    However, I find the best way to learn is to just try. I use Bash for everything I do, I try to avoid using graphical file browsers like Gnome's Nautilus, KDE's Dolphin, or Xfce's Thunar. Since I use bash all the time, very often I encounter a task I would like to perform, like rename only certain files, or rename a script and any reference to it using sed in any other script that uses it. So I spend a few minutes looking up how to do it on Google. Sometimes, just the man pages are enough. If you rely on the GUI too often, you will not learn.

    In the end, most everything can be done with "sed" for simple editing, a "foreach" loop or the "xargs" program for operating on lists of items, a "while read" loop for operating on lists of items written into files, "grep" for search, and "find" for a more advanced search. Learn how to use "Vi" or "Emacs" instead of "Nano", because they have syntax coloring and have other handy tools built-in to help you edit scripts more easily, like auto-indent and bookmarks. "Vi" even has it's own version of "sed" built-in, which is used as it's "find/replace" command, and Emacs has a similar "replace-regex" feature.

    With the right tools, you can write a 3-4 line script in less than two minutes to accomplish nearly anything. But like any computer system, it takes time and regular use to learn.

Posting Permissions

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