本站内容有下面分类知识,欢迎您的到来^_^

shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言

设soyo123为首页 收藏本站
当前位置:|主页>shell相关E文资料>

#34 Emulating GNU-Style Flags with Quota

百度收藏 QQ搜藏

The inconsistency between the command flags of various Unix systems is a perpetual problem and causes lots of grief for users who switch between any of the major releases, particularly between a commercial Unix (Solaris, HP-UX, and so on) and an open source Linux system. One command that demonstrates this problem is quota, which supports full-word flags on some Unix systems, while on others it accepts only one-letter flags.

A succinct shell script solves the problem, however, by mapping any full-word flags specified into the equivalent single-letter alternatives:

#!/bin/sh
# newquota - A front end to quota that works with full-word flags a la GNU.

# quota has three possible flags, -g, -v, and -q, but this script
#   allows them to be '--group', '--verbose', and '--quiet' too:

flags=""
realquota="/usr/bin/quota"

while [ $# -gt 0 ]
do
  case $1
  in
    --help          )  echo "Usage: $0 [--group --verbose --quiet -gvq]" >&2
                       exit 1 ;;
    --group | -group)  flags="$flags -g";       shift ;;
    --verbose | -verbose)  flags="$flags -v";   shift ;;
    --quiet | -quiet)  flags="$flags -q";       shift ;;
    --              )  shift;           break ;;
    *               )  break;          # done with 'while' loop!
  esac
done

exec $realquota $flags "$@"

How It Works
Did you notice that this script accepts both single- and double-dash prefixes for full words, making it actually a bit more flexible than the standard open source version, which insists on a single dash for one-letter flags and a double dash for full-word flags? With wrappers, the sky's the limit in terms of improved usability and increased consistency across commands.

Running the Script
There are a couple of ways to integrate a wrapper of this nature into your system. The most obvious is to rename the base quota command, rename this script quota, and then change the value of the realquota variable set at the beginning of the script. But you can also ensure that users have a PATH that looks in local directories before it looks in the standard Unix binary distro directories (e.g., /usr/local/bin before /bin and /usr/bin), which relies on the safe assumption that each user's PATH will see the script before it sees the real command. A third way is to add systemwide aliases so that a user typing quota actually invokes the newquota script.

The Results
$ newquota --verbose
Disk quotas for user dtint (uid 24810):
     Filesystem   usage   quota   limit   grace   files   quota   limit   grace
           /usr  338262  614400  675840           10703  120000  126000
$ newquota -quiet

The -q (quiet) mode emits output only if the user is over quota. You can see that this is working correctly from the last result because I'm not over quota.


上一篇:#33 Wrapping Only Long Lines 下一篇:#35 Making sftp Look More Like ftp
power by soyo123 2007-2008