使用参数中的通配符选项为bash脚本创建别名

时间:2021-11-26 07:37:35

I wanted to create the alias for the a bash script that make use of wild card as an argument. When I am trying to make use of the alias cmd its not giving the required output.

我想为使用外卡作为参数的bash脚本创建别名。当我尝试使用别名cmd时,它没有提供所需的输出。

The usage of the alias cmd will be log_list /tmp/abc*

别名cmd的用法是log_list / tmp / abc *

   #usage log_list /tmp/abc*
   alias log_list=`sh scriptname $1`

   #here is the script code
   for file_name in $* ; do
      if [ ! -d $file_name ] && [ -f $file_name ] ; then

          #do some processing ...
          echo $file_name
      fi
   done

1 个解决方案

#1


Aliases don't handle arguments via $1 and the like; they just prepend their text directly to the rest of the command line.

别名不通过$ 1等处理参数;他们只是将文本直接添加到命令行的其余部分。

Either use:

alias log_list='sh scriptname'     # note that these are single-quotes, not backticks

...or a function:

......或功能:

log_list() { sh scriptname "$@"; }

...though if your script is named log_list, marked executable, and located somewhere in the PATH, that alias or function should be completely unnecessary.

...但是,如果您的脚本名为log_list,标记为可执行文件,并且位于PATH中的某个位置,则该别名或函数应该完全没必要。


Now, that said, your proposed implementation of log_list also has a bunch of bugs. A cleaned-up version might look more like...

现在,也就是说,你提出的log_list实现也有一堆错误。清理后的版本可能更像......

#!/bin/sh
for file_name in "$@" ; do
   if [ ! -d "$file_name" ] && [ -f "$file_name" ] ; then
       #do some processing ...
       printf '%s\n' "$file_name"
   fi
done

#1


Aliases don't handle arguments via $1 and the like; they just prepend their text directly to the rest of the command line.

别名不通过$ 1等处理参数;他们只是将文本直接添加到命令行的其余部分。

Either use:

alias log_list='sh scriptname'     # note that these are single-quotes, not backticks

...or a function:

......或功能:

log_list() { sh scriptname "$@"; }

...though if your script is named log_list, marked executable, and located somewhere in the PATH, that alias or function should be completely unnecessary.

...但是,如果您的脚本名为log_list,标记为可执行文件,并且位于PATH中的某个位置,则该别名或函数应该完全没必要。


Now, that said, your proposed implementation of log_list also has a bunch of bugs. A cleaned-up version might look more like...

现在,也就是说,你提出的log_list实现也有一堆错误。清理后的版本可能更像......

#!/bin/sh
for file_name in "$@" ; do
   if [ ! -d "$file_name" ] && [ -f "$file_name" ] ; then
       #do some processing ...
       printf '%s\n' "$file_name"
   fi
done