Bash脚本:内部功能别名不再争论 [英] Bash script: internal function alias for longer arguments

查看:180
本文介绍了Bash脚本:内部功能别名不再争论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经定义了一个数组,像这样:

Suppose I have defined an array, like this:

DIR =(A B Supercalifragilistic)

和我需要调用脚本

./脚本A B Supercalifragilistic

这里的参数是通过内部功能处理的 func1的() FUNC2()。有没有一种方法,使一个别名(或任何东西,但它的称呼)取值 Supercalifragilistic 所以,当我调用

where the arguments are processed by internal functions func1() and func2(). Is there a way to make an alias (or anything, however it's called) S for Supercalifragilistic so that when I invoke:

./脚本A B小号

的内部功能将处理/间preT 取值 Supercalifragilistic

the internal functions will process/interpret S as Supercalifragilistic?

感谢您提前。

我要补充一点,脚本调用通过终端,而不是一个脚本里面,参数 AB Supercalifragilistic ,或(希望)取值,被传递到终端中的脚本。我很抱歉的混乱。

I should add that the script is invoked via terminal, not inside a script, and the arguments A B Supercalifragilistic, or (hopefully) S, are passed on to the script in the terminal. I'm sorry for the confusion.

该脚本是在这里:<一href=\"http://stackoverflow.com/questions/23155046/bash-script-if-any-argument-is-n-then-function-has-extra-options/23179861#23179861\">Bash脚本:如果任何参数是&QUOT; N&QUOT;然后函数有额外的选项,在下面的答案。它所做的是在解释OP那里,下面的脚本。最后,而不是的DIR =(ABCDEF) DIR =(AB澄清DEF)(这只是一个例子)和文件夹澄清是比其他人不同的路径只有一个。我希望这是更清楚了吧,如果不是,请告诉我。

The script is here: Bash script: if any argument is "N" then function has extra options , in the answer below. What it does is explained in the OP there, below the script. Finally, instead of DIR=(A B C D E F) it's DIR=(A B Clarification D E F) (it's just an example) and the folder Clarification is the only one in a different path than the rest. I hope it's more clear now, if not, please tell me.

我想我可以喊Evrika!。你的话硬codeD让我意识到我的有无的修改脚本随时一个新的文件夹被添加/删除的,所以我想使该数组动态,如结果
./脚本BD EG 的结果 =阵列(A BD EG)结果
而且它应该更换一些短的人的长路径(澄清&GT;&GT;ç),所以我做了基于也在这里解答这个测试脚本:

I think I can shout "Evrika!". Your word "hardcoded" made me realize I have to modify the script anytime a new folder gets added/deleted, so I thought of making the array dynamic, as in
./script a b "d e" g results in array=(a b "d e" g)
but also that it should replace the long paths with some short ones (Clarification >> C), so I made this test script based on also the answers here:

#!/bin/bash

array=()

for i in "$@"
do
    if [[ "$i" == C ]]
    then
        array+=("Clarification")
    else
        array+=("$i")
    fi
done

echo ${array[*]}
echo

for i in $(seq 0 $(( $# - 1 )))
do
    echo ${array["$i"]}
done

和这是它显示了在命令提示符下:

and this is what it shows at command prompt:

$ ./x.sh abc C "d f" e
abc Clarification d f e

abc
Clarification
d f
e

我想现在我终于可以让脚本做我想做的。谢谢你,总之,您的答案。

I think now I can finally make the script to do what I want. Thank you, all, for the answers.

推荐答案

我真的不知道你到底要实现!但是我看了一下你上次编辑链接的脚本。既然你有一个硬codeD数组,你不妨改用关联数组:

I really have no idea what you exactly want to achieve! But I had a look at the script you linked in your last edit. Since you have a hard-coded array you might as well instead use an associative array:

declare -A dir_h
dir_h["A"]=A
dir_h["B"]=B
dir_h["C"]=../path/Clarification
dir_h["D"]=D
dir_h["E"]=E

dir_h 的钥匙,即环上的 A B C D E

for k in "${!dir_h[@]}"; do
    echo "$k => ${dir_h[$k]}"
done

试试吧,这可能帮助你与你的别名问题(或没有)。

Try it, this might help you with your "alias" problem (or not).

这是你的脚本,从您的其他职务,使用这种技术,并在一个更加一致和可读的形式(注:我还没有尝试过,可能有一些轻微的错别字,让我知道,如果是这种情况):

Here's your script from your other post, using this technique and in a more consistent and readable form (note: I haven't tried it, there might be some minor typos, let me know if it's the case):

#!/bin/bash
# ./test.sh     = 1. searches for existing archives
#               1.a. if they exist, it backups them into BKP/.
#               1.b. if not, displays a message
#             2. archives all the directories in the array list
# ./test.sh N       = 1. deletes all the folder's archives existent and
#           specified in the array list
#             2. archives all the directories in the array list
# ./test.sh {A..F}  = 1. searches for existing archives from arguments
#               1.a. if they exist, it backups them into BKP/.
#               1.b. if not, displays a message
#             2. archives all the directories passed as arguments
# ./test.sh {A..F} N    = 1. deletes all the archives matching $argument.zip
#             2. archives all the directories passed as arguments

# The directories to be backed-up/archived, all in the current (script's) path
# except "C", on a different path

declare -A dir_h
dir_h["A"]=A
dir_h["B"]=B
dir_h["C"]=../path/Clarification
dir_h["D"]=D
dir_h["E"]=E
dir_h["F"]=F

declare -A nope_h
nope_h["A"]=bogus
nope_h["B"]=bogus
nope_h["C"]=nope
nope_h["D"]=bogus
nope_h["E"]=bogus
nope_h["F"]=bogus

die() {
   (($#)) && printf >&2 "%s\n" "$@"
   exit 1
}

bak() {

   if [[ "$1" != N ]]; then
      # Check that arg is in dir list:
      [[ -n ${dir_h["$1"]} ]] || die "Error in bak: argument \`$1' not handled"
      if [[ -f $1.zip ]]; then
         mv -vi "$1.zip" "BKP/$1.zip_$(date +"%H-%M")" || die
      else
         echo "$(tput setaf 1) no $1.zip$(tput sgr0)"
      fi
   fi
}

# The archive function, if any argument is "N", processing it is omitted. Folder
# "C" has special treatment
archive() {
   if [[ $1 != N ]]; then
      7z a -mx=9 "$1.zip" "${dir_h["$1"]}" -r -x\!"$1/${nope_h["$1"]}" || die
   fi
}

# Let's check once for all whether N is in the arg list
foundN=0
for a in "$@"; do [[ $a = N ]] && foundN=1 && break; done

if (($#==0)); then
   # case #1: no arguments
   for d in "${!dir_h[@]}"; do
      echo "$(tput setaf 2) backup$(tput sgr0)"
      bak "$d"
      archive "$d"
   done
elif (($#==1)) && ((foundN)); then
   # case #2: one argument, "N"
   for d in "${!dir_h[@]}"; do
      echo "$(tput setaf 1) no backup needed, removing$(tput sgr0)"
      rm -v "$d".zip || die
      archive "$d"
   done
elif (($#>1)) && ((foundN)); then
   # case #3: folders as arguments with "N"
   for f in "$@"; do
      if [[ $f != N ]]; then
         echo "$(tput setaf 1) no backup needed, removing$(tput sgr0)"
         rm -v "$f.zip" || die
      fi
      archive "$f"
   done
else
   for f in "$@"; do
      echo "$(tput setaf 2) backup$(tput sgr0)"
      bak "$f"
      archive "$f"
   done
fi

从这里就可以做很多事情,并有pretty无限多的别名处理的可能性。

From this you can do a lot, and have pretty much infinite "alias" handling possibilities.

这篇关于Bash脚本:内部功能别名不再争论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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