made_in_india
made_in_india

Reputation: 2279

creating alias for a bash script with a wild card option in the argument

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.

The usage of the alias cmd will be 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

Upvotes: 2

Views: 64

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295472

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

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.


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

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

Upvotes: 4

Related Questions