1

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.

Striketh
  • 559
  • 1
  • 5
  • 17
  • 1
    This is strange. The code already demonstrates knowledge and a good example of a function. Why not leverage that knowledge? – wallyk Jul 06 '12 at 20:15

4 Answers4

2

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.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • You're right. I knew it was something like that, but couldn't remember the name. Looks much better now :) – Striketh Jul 06 '12 at 20:21
1

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.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
1
#!/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
Luc M
  • 16,630
  • 26
  • 74
  • 89
  • Thanks for writing it out. I ended up doing it myself, but giving the answer to you for the extra effort. – Striketh Jul 06 '12 at 20:21
0

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!

user1277476
  • 2,871
  • 12
  • 10
  • It doesn't answer the question but it's good to know! see http://stackoverflow.com/questions/2188199/bash-double-or-single-bracket-parentheses-curly-braces – Luc M Jul 07 '12 at 04:10