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.

1
  • 1
    This is strange. The code already demonstrates knowledge and a good example of a function. Why not leverage that knowledge? Commented Jul 6, 2012 at 20:15

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. I knew it was something like that, but couldn't remember the name. Looks much better now :)
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

1 Comment

Thanks for writing it out. I ended up doing it myself, but giving the answer to you for the extra effort.
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.

Comments

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!

1 Comment

It doesn't answer the question but it's good to know! see stackoverflow.com/questions/2188199/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.