Συνήθιζα να χρησιμοποιώ το CShell ([tag:csh]), το οποίο σας επιτρέπει να δημιουργήσετε ένα ψευδώνυμο που λαμβάνει μια παράμετρο. Ο συμβολισμός ήταν κάτι σαν
alias junk="mv \\!* ~/.Trash"
Στο Bash, αυτό δεν φαίνεται να λειτουργεί. Δεδομένου ότι ο Bash έχει ένα πλήθος χρήσιμων χαρακτηριστικών, θα υπέθετα ότι αυτό έχει υλοποιηθεί, αλλά αναρωτιέμαι πώς.
Το ψευδώνυμο Bash δεν δέχεται άμεσα παραμέτρους. Θα πρέπει να δημιουργήσετε μια συνάρτηση.
Το alias
δεν δέχεται παραμέτρους, αλλά μια συνάρτηση μπορεί να κληθεί ακριβώς όπως ένα ψευδώνυμο. Για παράδειγμα:
myfunction() {
#do things with parameters like $1 such as
mv "$1" "$1.bak"
cp "$2" "$1"
}
myfunction old.conf new.conf #calls `myfunction`
Παρεμπιπτόντως, οι συναρτήσεις Bash που ορίζονται στο .bashrc
και σε άλλα αρχεία είναι διαθέσιμες ως εντολές μέσα στο κέλυφος σας. Έτσι, για παράδειγμα, μπορείτε να καλέσετε την προηγούμενη συνάρτηση ως εξής
$ myfunction original.conf my.conf
Εξειδικεύοντας την παραπάνω απάντηση, μπορείτε να έχετε σύνταξη 1 γραμμής όπως μπορείτε για τα ψευδώνυμα, η οποία είναι πιο βολική για ad-hoc ορισμούς σε ένα κέλυφος ή αρχεία .bashrc:
bash$ myfunction() { mv "$1" "$1.bak" && cp -i "$2" "$1"; }
bash$ myfunction original.conf my.conf
Μην ξεχνάτε την άνω τελεία πριν από την κλειστή δεξιά αγκύλη. Ομοίως, για την πραγματική ερώτηση:
csh% alias junk="mv \\!* ~/.Trash"
bash$ junk() { mv "$@" ~/.Trash/; }
Ή:
bash$ junk() { for item in "$@" ; do echo "Trashing: $item" ; mv "$item" ~/.Trash/; done; }
Εδώ είναι τρία παραδείγματα συναρτήσεων που έχω στο ~/.bashrc
μου, που είναι ουσιαστικά ψευδώνυμα που δέχονται μια παράμετρο:
#Utility required by all below functions.
#https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable#comment21953456_3232433
alias trim="sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g'"
.
:<<COMMENT
Alias function for recursive deletion, with are-you-sure prompt.
Example:
srf /home/myusername/django_files/rest_tutorial/rest_venv/
Parameter is required, and must be at least one non-whitespace character.
Short description: Stored in SRF_DESC
With the following setting, this is *not* added to the history:
export HISTIGNORE="*rm -r*:srf *"
- https://superuser.com/questions/232885/can-you-share-wisdom-on-using-histignore-in-bash
See:
- y/n prompt: https://stackoverflow.com/a/3232082/2736496
- Alias w/param: https://stackoverflow.com/a/7131683/2736496
COMMENT
#SRF_DESC: For "aliaf" command (with an 'f'). Must end with a newline.
SRF_DESC="srf [path]: Recursive deletion, with y/n prompt\n"
srf() {
#Exit if no parameter is provided (if it's the empty string)
param=$(echo "$1" | trim)
echo "$param"
if [ -z "$param" ] #http://tldp.org/LDP/abs/html/comparison-ops.html
then
echo "Required parameter missing. Cancelled"; return
fi
#Actual line-breaks required in order to expand the variable.
#- https://stackoverflow.com/a/4296147/2736496
read -r -p "About to
sudo rm -rf \"$param\"
Are you sure? [y/N] " response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y)$ ]]
then
sudo rm -rf "$param"
else
echo "Cancelled."
fi
}
.
:<<COMMENT
Delete item from history based on its line number. No prompt.
Short description: Stored in HX_DESC
Examples
hx 112
hx 3
See:
- https://unix.stackexchange.com/questions/57924/how-to-delete-commands-in-history-matching-a-given-string
COMMENT
#HX_DESC: For "aliaf" command (with an 'f'). Must end with a newline.
HX_DESC="hx [linenum]: Delete history item at line number\n"
hx() {
history -d "$1"
}
.
:<<COMMENT
Deletes all lines from the history that match a search string, with a
prompt. The history file is then reloaded into memory.
Short description: Stored in HXF_DESC
Examples
hxf "rm -rf"
hxf ^source
Parameter is required, and must be at least one non-whitespace character.
With the following setting, this is *not* added to the history:
export HISTIGNORE="*hxf *"
- https://superuser.com/questions/232885/can-you-share-wisdom-on-using-histignore-in-bash
See:
- https://unix.stackexchange.com/questions/57924/how-to-delete-commands-in-history-matching-a-given-string
COMMENT
#HXF_DESC: For "aliaf" command (with an 'f'). Must end with a newline.
HXF_DESC="hxf [searchterm]: Delete all history items matching search term, with y/n prompt\n"
hxf() {
#Exit if no parameter is provided (if it's the empty string)
param=$(echo "$1" | trim)
echo "$param"
if [ -z "$param" ] #http://tldp.org/LDP/abs/html/comparison-ops.html
then
echo "Required parameter missing. Cancelled"; return
fi
read -r -p "About to delete all items from history that match \"$param\". Are you sure? [y/N] " response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y)$ ]]
then
#Delete all matched items from the file, and duplicate it to a temp
#location.
grep -v "$param" "$HISTFILE" > /tmp/history
#Clear all items in the current sessions history (in memory). This
#empties out $HISTFILE.
history -c
#Overwrite the actual history file with the temp one.
mv /tmp/history "$HISTFILE"
#Now reload it.
history -r "$HISTFILE" #Alternative: exec bash
else
echo "Cancelled."
fi
}
Αναφορές: