Striketh
Striketh

Reputation: 559

Reduce repeating of commands in bash script

I'm working on a bash script to increase /tmp on a VPS server and I'd like to make it cleaner/more efficient without the need to repeat the same commands for whichever option is picked. Here's how it's structured right now:

#!/bin/bash

  showMenu () {
        echo "1) Increase /tmp size to 1 GB"
        echo "2) Increase /tmp size to 2 GB"
        echo "3) Quit"
}
   while [ 1 ]
   do
       showMenu
       read CHOICE
       case "$CHOICE" in

  "1")
       /etc/init.d/mysql stop
       /etc/init.d/httpd stop
       /etc/init.d/cpanel stop
       cp -af /var/tmp /var/tmp.bak
       umount -l /var/tmp
       umount -l /tmp
       rm -f /usr/tmpDSK
       dd if=/dev/zero of=/usr/tmpDSK bs=1M count=1k
       mkfs.ext3 -F /usr/tmpDSK
       mount -t ext3 -o nosuid,noexec,loop /usr/tmpDSK /tmp
       mount -o bind,noexec,nosuid /tmp /var/tmp
       cp -a /var/tmp.bak/* /tmp/
       rm -rf /var/tmp.bak/
       chmod 1777 /tmp
       /etc/init.d/mysql start
       /etc/init.d/httpd start
       /etc/init.d/cpanel start
       df -h
       exit 1
       ;;

    "2")
       /etc/init.d/mysql stop
       /etc/init.d/httpd stop
       /etc/init.d/cpanel stop
       cp -af /var/tmp /var/tmp.bak
       umount -l /var/tmp
       umount -l /tmp
       rm -f /usr/tmpDSK
       dd if=/dev/zero of=/usr/tmpDSK bs=1M count=2k
       mkfs.ext3 -F /usr/tmpDSK
       mount -t ext3 -o nosuid,noexec,loop /usr/tmpDSK /tmp
       mount -o bind,noexec,nosuid /tmp /var/tmp
       cp -a /var/tmp.bak/* /tmp/
       rm -rf /var/tmp.bak/
       chmod 1777 /tmp
       /etc/init.d/mysql start
       /etc/init.d/httpd start
       /etc/init.d/cpanel start
       df -h
       exit 1
       ;;

    "3")
        exit 1
       ;;
      esac
    done

I basically want to only initiate the redundant commands once during this process. Can you give me an idea or ideas on the best way to do this?

Thanks.

Upvotes: 1

Views: 778

Answers (4)

user1277476
user1277476

Reputation: 2909

Note that bash functions are a little weird.

function abc
{
   global_var=1
}

function def
(
   local_var=1
)

Note the curly braces vs parens. With the parens, your function is run in a subshell, while with the curly braces, your function does not get a unique namespace!

Upvotes: 0

Luc M
Luc M

Reputation: 17314

#!/bin/bash

function size_up {
           /etc/init.d/mysql stop
           /etc/init.d/httpd stop
           /etc/init.d/cpanel stop
           cp -af /var/tmp /var/tmp.bak
           umount -l /var/tmp
           umount -l /tmp
           rm -f /usr/tmpDSK
           dd if=/dev/zero of=/usr/tmpDSK bs=1M count=${1}k
           mkfs.ext3 -F /usr/tmpDSK
           mount -t ext3 -o nosuid,noexec,loop /usr/tmpDSK /tmp
           mount -o bind,noexec,nosuid /tmp /var/tmp
           cp -a /var/tmp.bak/* /tmp/
           rm -rf /var/tmp.bak/
           chmod 1777 /tmp
           /etc/init.d/mysql start
           /etc/init.d/httpd start
           /etc/init.d/cpanel start
           df -h
           exit 1
           ;;
}



function showMenu () {
        echo "1) Increase /tmp size to 1 GB"
        echo "2) Increase /tmp size to 2 GB"
        echo "3) Quit"
}
 while [ 1 ]
   do
    showMenu
    read CHOICE
    case "$CHOICE" in
       "1") size_up 1
       "2") size_up 2
       "3") exit 1
       ;;
    esac
done

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 359965

This is what functions are for. Put your common code in a function and call it from the appropriate case selections. An alternative is to set flags in your case statement and do conditional execution based on the flags.

Upvotes: 1

Alex Howansky
Alex Howansky

Reputation: 53533

Sounds like a good fit for a bash function. Put the meat of the code in a function and then pass a parameter for the size.

Upvotes: 2

Related Questions