Linux 中带有解析参数的 Bash 脚本 [英] Bash Script with Parsing Argument in Linux

查看:32
本文介绍了Linux 中带有解析参数的 Bash 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个 bash 脚本:

I want to write a bash script :

schedsim.sh [-h] [-c x] -i pathfile

哪里:

• -h:打印当前用户名.

• -h: print the current username.

• -c x :获取选项参数 x 并打印出 (x + 1).如果没有找到参数,则打印默认值为 1.

• -c x : get an option-argument x and print out (x + 1). If no argument was found, print the default value is 1.

• -i pathfile:打印路径文件的大小.路径文件是必需的参数.如果未找到任何参数,则打印出错误消息.

• -i pathfile: print the size of pathfile. The pathfile is a required argument. If no argument was found, print out an error message.

这是我到目前为止所做的:

This is what I've done so far :

x=""
path=""
while getopts ":hc:i:" Option
do
case $Option in
h) echo -e "$USER\n"
;;
c) x=$optarg+1
;;
i) path=$(wc -c <"$optarg")
;;
esac
done

if [ -z "$x"] 
then
 echo -e "$x\n"
else
 echo 1
fi

if [ -z "$path"] 
then
 echo $path
else
 echo "Error Message"
 exit 1
fi

如何完成option-argument、required-argument部分和错误信息部分?

How to finish the option-argument,required-argument part and the error message part?

推荐答案

重写:

while getopts ":hc:i:" Option; do
    case $Option in
        h)  echo "$USER"
            ;;
        c)  x=$(($OPTARG + 1))
            ;;
        i)  if [[ -f $OPTARG ]]; then
                size=$(wc -c <"$OPTARG")
            else
                echo "error: no such file: $OPTARG"
                exit 1
            fi 
            ;;
    esac
done

if [[ -z $x ]]; then
    echo "you used -c: the result is $x"
fi

if [[ -z $size ]]; then
    echo "you used -i: the file size is $size"
fi

注意事项:

  • OPTARG 必须大写.
  • 你需要 $((...)) 进行 bash 算法
  • 在使用前检查文件是否存在
  • 使用合理的文件名(path 不代表文件的大小)
  • 必须]前有一个空格
  • echo -e "value\n" 工作量太大:你只需要 echo "value" 除非你想处理值中的转义序列:

  • OPTARG must be in upper case.
  • you need $((...)) for bash arithmetic
  • check that the file exists before using it
  • use a sensible filename (path does not represent the size of a file)
  • there must be a space before ]
  • echo -e "value\n" is too much work: you just want echo "value" unless you want to process escape sequences in the value:

$ var="foo\tbar\rbaz"
$ echo "$var"
foo\tbar\rbaz
$ echo -e "$var\n"
baz bar

$ 

更新:回复评论:简化和更完整的选项处理;扩展错误处理.

Update: responding to comments: simplified and more complete option handling; expanded error handling.

#!/bin/bash
declare -A given=([c]=false [i]=false)
x=0
path=""

while getopts ":hc:i:" Option; do
    case $Option in
        h)  echo "$USER"
            ;;
        c)  x=$OPTARG
            given[c]=true
            ;;
        i)  path=$OPTARG
            given[i]=true
            ;;
        :)  echo "error: missing argument for option -$OPTARG"
            exit 1
            ;;
        *)  echo "error: unknown option: -$OPTARG"
            exit 1
            ;;
    esac
done

# handle $x
if [[ ! $x =~ ^[+-]?[[:digit:]]+$ ]]; then
    echo "error: your argument to -c is not an integer"
    exit 1
fi
if ! ${given[c]}; then
    printf "using the default value: "
fi
echo $(( 10#$x + 1 ))

# handle $path
if ! ${given[i]}; then
    echo "error: missing mandatory option: -i path"
    exit 1
fi
if ! [[ -f "$path" ]]; then
    echo "error: no such file: $path"
    exit 1
fi
echo "size of '$path' is $(stat -c %s "$path")"

这篇关于Linux 中带有解析参数的 Bash 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆