减少重写bash脚本中的命令

时间:2012-07-06 20:09:19

标签: linux bash scripting

我正在使用一个bash脚本来增加/ tmp在VPS服务器上的运行,我想让它更干净/更高效,而无需为选择的任何选项重复相同的命令。以下是它现在的结构:

#!/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

我基本上只想在此过程中启动一次冗余命令。你能给我一个关于最佳方法的想法或想法吗?

感谢。

4 个答案:

答案 0 :(得分:2)

听起来非常适合bash function。将代码的内容放在函数中,然后传递大小的参数。

答案 1 :(得分:1)

这就是函数的用途。将您的公共代码放在一个函数中,并从相应的case选项中调用它。另一种方法是在case语句中设置标志,并根据标志执行条件执行。

答案 2 :(得分: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

答案 3 :(得分:0)

请注意,bash函数有点奇怪。

function abc
{
   global_var=1
}

function def
(
   local_var=1
)

请注意花括号与parens。使用parens,您的函数在子shell中运行,而使用花括号,您的函数不会获得唯一的命名空间!