Join the community today
Register Now

Create a bash script

Discussion in 'System Administration' started by pamamolf, Jun 23, 2014.

Tags:
  1. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Hi :)

    Some times i install software using 20 command lines about ...

    Is it easy for a noob like me to automate the proccess using a bash script?

    Thanks

     
  2. Dan

    Dan New Member

    14
    4
    3
    Jun 22, 2014
    Ratings:
    +5
    Local Time:
    8:15 AM
  3. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Thanks Dan but that doesn't help a lot :)
     
  4. eva2000

    eva2000 Administrator Staff Member

    54,368
    12,198
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,763
    Local Time:
    11:15 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    That linked guide is one of the easier examples for bash shell scripts http://www.linux.com/learn/tutorials/284789-writing-a-simple-bash-script- can not get any easier to be honest

    it's basically add to top of file

    Code:
    #/bin/bash
    then list your commands below
    Code:
    #/bin/bash
    your commands
    i.e. commands to download and install .06 stable release outlined at http://centminmod.com/download.html would become

    Code:
    #/bin/bash
    cd /usr/local/src
    wget http://centminmod.com/download/centmin-v1.2.3-eva2000.06.zip
    
    unzip centmin-v1.2.3-eva2000.06.zip
    cd centmin-v1.2.3mod
    chmod +x centmin.sh
    
    yum -y install bc wget yum-plugin-fastestmirror
    yum clean all
    
    ./centmin.sh
     
  5. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Do i need to leave a space between commands as you post above?

    Also if i want to install more than one software can i continue adding lines there?

    The script will auto wait for one command to finish to run the next one just adding the lines as you did above?

    Thanks !!!!!
     
  6. eva2000

    eva2000 Administrator Staff Member

    54,368
    12,198
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,763
    Local Time:
    11:15 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    bash won't recognise line breaks, so no difference either way

    yes it's sequential order according to command listed so you could technically add more commands afterwards but not if you have ./centmin.sh as that invokes the menu.

    If using .07 beta you can

    change the line

    from
    Code:
    ./centmin.sh
    
    to
    Code:
    ./centmin.sh install
    
    to bypass menu based install for CLI unattended install
    If you download .07 beta21 zip you will see an installer.sh file which is basically a more advanced version of what I listed above for installing .07 beta21 via installer.sh
     
    Last edited: Jun 23, 2014
  7. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    One last question :)

    How can i edit using bash script a file for example /home/script/file.conf and replace line 15 with my line ?

    Lets say that line 15 is server=bad and i want to change this with server=good .

    Thanks
     
  8. eva2000

    eva2000 Administrator Staff Member

    54,368
    12,198
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,763
    Local Time:
    11:15 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    use either nano or vim linux text editors :)

    For nano linux text editor, you can read up more about nano here and here.
     
  9. dorobo

    dorobo Active Member

    420
    104
    43
    Jun 6, 2014
    Ratings:
    +162
    Local Time:
    9:15 PM
    latest
    latest
    use sed, something like

    sed -i 's/server=bad/server=good/g' /home/script/file.conf

    try looking at the centminmod script and reverse engineer it.
     
  10. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    I know that i must run :

    nano /home/script/file.conf

    But how can i do the rest is my problem :)

    @dorobo thanks :)
     
  11. eva2000

    eva2000 Administrator Staff Member

    54,368
    12,198
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,763
    Local Time:
    11:15 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    yeah you can use sed to replace lines too which Centmin Mod uses alot of :D

    Practice and trial and error only way - setup test VPS or install Virtualbox on local pc to setup CentOS for testing Centmin Mod and bash scripting ;)
     
  12. dorobo

    dorobo Active Member

    420
    104
    43
    Jun 6, 2014
    Ratings:
    +162
    Local Time:
    9:15 PM
    latest
    latest
    sed -i 's/server=bad/server=good/g' /home/script/file.conf
     
  13. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    @dorobo sorry i just check it after posting the above.....

    Thanks

    If i want to add something on a line 20 that is empty how is the correct syntax using sed?
     
  14. eva2000

    eva2000 Administrator Staff Member

    54,368
    12,198
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,763
    Local Time:
    11:15 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  15. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    wow super :)

    do i have to do anything else after that command to save and exit the file that i just edit?
     
  16. eva2000

    eva2000 Administrator Staff Member

    54,368
    12,198
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,763
    Local Time:
    11:15 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    sed -i modifies the file already
     
  17. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Replace the first line of a file:

    sed '1 c\
    > The Geek Stuff' thegeekstuff.txt

    Does that means:

    sed '1 c\> The Geek Stuff' thegeekstuff.txt

    or i do not need the > ?
     
  18. eva2000

    eva2000 Administrator Staff Member

    54,368
    12,198
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,763
    Local Time:
    11:15 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    would end up as

    Code:
    sed -i "1c replace 1" test.txt   
    
    cat test.txt                  
    replace 1
    2
    3
    5
    6
    4
     
  19. pamamolf

    pamamolf Premium Member Premium Member

    4,077
    427
    83
    May 31, 2014
    Ratings:
    +833
    Local Time:
    3:15 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    What i must add to bash so i can log installation for errors?

    Searching i found this:

    Code:
    exec "${@}" > >(tee logfile) 2>&1
    But does this will display the errors in console also (real time) or not?
     
    Last edited: Jun 23, 2014