Make a Bash alias that takes a parameter?

后端 未结 20 1928
长发绾君心
长发绾君心 2020-11-21 06:53

I used to use CShell (csh), which lets you make an alias that takes a parameter. The notation was something like

alias junk=\"mv \\\\!* ~/.Trash\"

20条回答
  •  清酒与你
    2020-11-21 07:17

    Once i did some fun project and i still use it. It's showing some animation while i copy files via cp command coz cp don't show anything and it's kind of frustrating. So i made this alias

    alias cp="~/SCR/spiner cp"
    

    And this is the spiner script

    #!/bin/bash
    
    #Set timer
    T=$(date +%s)
    
    #Add some color
    . ~/SCR/color
    
    #Animation sprites
    sprite=( "(* )  ( *)" " (* )( *) " " ( *)(* ) " "( *)  (* )" "(* )  ( *)" )
    
    #Print empty line and hide cursor
    printf "\n${COF}"
    
    #Exit function
    function bye { printf "${CON}"; [ -e /proc/$pid ] && kill -9 $pid; exit; }; trap bye INT
    
    #Run our command and get its pid
    "$@" & pid=$!
    
    #Waiting animation
    i=0; while [ -e /proc/$pid ]; do sleep 0.1
    
        printf "\r${GRN}Please wait... ${YLW}${sprite[$i]}${DEF}"
        ((i++)); [[ $i = ${#sprite[@]} ]] && i=0
    
    done
    
    #Print time and exit
    T=$(($(date +%s)-$T))
    printf "\n\nTime taken: $(date -u -d @${T} +'%T')\n"
    
    bye
    

    It's look like this

    Cycled animation)

    Here is the link to a color script mentioned above. And new animation cycle)

提交回复
热议问题