kzen.dev
  • Ερωτήσεις
  • Ετικέτες
  • Χρήστες
Ειδοποιήσεις
Ανταμοιβές
Εγγραφή
Μόλις εγγραφείτε, θα ενημερωθείτε για τις απαντήσεις και τα σχόλια στις ερωτήσεις σας.
Συνδεθείτε
Αν έχετε ήδη λογαριασμό, συνδεθείτε για να ελέγξετε τις νέες ειδοποιήσεις.
Θα υπάρξουν ανταμοιβές για πρόσθετες ερωτήσεις, απαντήσεις και σχόλια.
Περισσότερα
Πηγή
Επεξεργασία
 Hello
Hello
Ερώτηση

Να δημιουργήσετε ένα ψευδώνυμο Bash που παίρνει μια παράμετρο;

Συνήθιζα να χρησιμοποιώ το CShell ([tag:csh]), το οποίο σας επιτρέπει να δημιουργήσετε ένα ψευδώνυμο που λαμβάνει μια παράμετρο. Ο συμβολισμός ήταν κάτι σαν

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

Στο Bash, αυτό δεν φαίνεται να λειτουργεί. Δεδομένου ότι ο Bash έχει ένα πλήθος χρήσιμων χαρακτηριστικών, θα υπέθετα ότι αυτό έχει υλοποιηθεί, αλλά αναρωτιέμαι πώς.

1155 2011-08-20T12:11:41+00:00 3
 codeforester
codeforester
Επεξεργασμένη ερώτηση Κάνε Μάρτιος 2017 в 7:32
Προγραμματισμός
alias
bash
Δημοφιλή βίντεο
Bash While Loop | Reading Files in Shell Script | File Read line by line Shell | Learn Bash Tutorial
Bash While Loop | Reading Files in Shell Script | File Read line by line Shell | Learn Bash Tutorial
πριν 1 χρόνος
Δημιουργήστε ένα ερώτημα με όνομα q1 που θα παίρνει στοιχεία από τους πίνακες Πελάτες και...
Δημιουργήστε ένα ερώτημα με όνομα q1 που θα παίρνει στοιχεία από τους πίνακες Πελάτες και...
πριν 9 χρόνια
Create An Easy to Use, Locally Hosted Bash Script Repository
Create An Easy to Use, Locally Hosted Bash Script Repository
πριν 1 χρόνος
HOW TO COUNT FILES IN A DIRECTORY IN BASH
HOW TO COUNT FILES IN A DIRECTORY IN BASH
πριν 3 χρόνια
[TM2033] Lab0S 06 Bash Script
[TM2033] Lab0S 06 Bash Script
πριν 3 χρόνια
Linux Shell Scripting Tutorial Part 2
Linux Shell Scripting Tutorial Part 2
πριν 1 χρόνος
Shell Scripting - 4 - Bash Functions and for-in Loops
Shell Scripting - 4 - Bash Functions and for-in Loops
πριν 3 χρόνια
Bash Script Variables - Understand Variables in the Shell Script - Ep02
Bash Script Variables - Understand Variables in the Shell Script - Ep02
πριν 1 χρόνος
Shell script wallpaper changer -simple and easy
Shell script wallpaper changer -simple and easy
πριν 1 χρόνος
5.Rules for Naming a Variable in Shell Script|Variables in Shell Script | Shell Scripting Tutorial
5.Rules for Naming a Variable in Shell Script|Variables in Shell Script | Shell Scripting Tutorial
πριν 2 χρόνια
How to Write Scripts on Linux (BASH)
How to Write Scripts on Linux (BASH)
πριν 2 χρόνια
73 Shell Scripting Tutorial for Beginners 28   How to debug a bash script
73 Shell Scripting Tutorial for Beginners 28 How to debug a bash script
πριν 4 χρόνια
Writing BASH script to print "Helloworld" - ~3Minutes: Tutorial
Writing BASH script to print "Helloworld" - ~3Minutes: Tutorial
πριν 2 χρόνια
Linux Cat Command || Bash Scripting || Shell Scripting || By Designer Code
Linux Cat Command || Bash Scripting || Shell Scripting || By Designer Code
πριν 1 χρόνος
[ bash ] Automating Boring Work With POSIX shell
[ bash ] Automating Boring Work With POSIX shell
πριν 1 χρόνος
« Προηγούμενο
Επόμενο »
Αυτή η ερώτηση έχει 1 απάντηση στα αγγλικά, για να τις διαβάσετε συνδεθείτε στο λογαριασμό σας.
Λύση / Απάντηση
 arunkumar
arunkumar
Κάνε Αύγουστος 2011 в 12:15
2011-08-20T12:15:04+00:00
Περισσότερα
Πηγή
Επεξεργασία
#13214825

Το ψευδώνυμο 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
 Aserre
Aserre
Επεξεργασμένη απάντηση Κάνε Απρίλιος 2019 в 8:24
1958
0
Mike Gleason
Mike Gleason
Κάνε Μάιος 2014 в 3:46
2014-05-14T15:46:52+00:00
Περισσότερα
Πηγή
Επεξεργασία
#13214826

Εξειδικεύοντας την παραπάνω απάντηση, μπορείτε να έχετε σύνταξη 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; }
Mike Gleason
Mike Gleason
Επεξεργασμένη απάντηση Κάνε Νοέμβριος 2018 в 9:28
194
0
 aliteralmind
aliteralmind
Κάνε Ιανουάριος 2015 в 3:57
2015-01-13T03:57:19+00:00
Περισσότερα
Πηγή
Επεξεργασία
#13214827

Εδώ είναι τρία παραδείγματα συναρτήσεων που έχω στο ~/.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
}

Αναφορές:

  • Αποκοπή λευκών κενών από συμβολοσειρές: https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable#comment21953456_3232433
  • Πραγματικές αλλαγές γραμμής: https://stackoverflow.com/a/4296147/2736496
  • Alias w/param: https://stackoverflow.com/a/7131683/2736496 (άλλη μια απάντηση σε αυτή την ερώτηση)
  • HISTIGNORE: https://superuser.com/questions/232885/can-you-share-wisdom-on-using-histignore-in-bash
  • Υ/Ν προτροπή: https://stackoverflow.com/a/3232082/2736496
  • Διαγραφή όλων των αντίστοιχων στοιχείων από το ιστορικό: https://unix.stackexchange.com/questions/57924/how-to-delete-commands-in-history-matching-a-given-string
  • Είναι συμβολοσειρά μηδέν/κενή: http://tldp.org/LDP/abs/html/comparison-ops.html
 Community
Community
Επεξεργασμένη απάντηση Κάνε Μάιος 2017 в 11:55
Other Comparison Operators
tldp.org
bash - How to delete commands in history matching a given string? - Unix &amp; Linux Stack Exchange
I need to delete all commands in my history matching a string. I&#x27;ve tried:&#xA;&#xA;$ history | grep searchstring | cut -d&quot; &quot; -f2 | history -d&#xA;-bash: history: -d: option requires an argument&#xA;&#xA;$ history | g...
unix.stackexchange.com
10
0
Προσθήκη ερώτησης
Κατηγορίες
Όλα
Τεχνολογία
Πολιτισμός / Αναψυχή
Ζωή / Τέχνες
Επιστήμη
Επαγγελματικό
Επιχείρηση
Χρήστες
Όλα
Νέα
Δημοφιλές
1
Daniel Gogov
Εγγεγραμμένη πριν 1 εβδομάδα
2
工藤 芳則
Εγγεγραμμένη πριν 2 εβδομάδες
3
Ирина Беляева
Εγγεγραμμένη πριν 2 εβδομάδες
4
Darya Arsenyeva
Εγγεγραμμένη πριν 2 εβδομάδες
5
anyta nuam-nuam (LapuSiK)
Εγγεγραμμένη πριν 3 εβδομάδες
DE
EL
ES
FR
ID
IT
JA
NL
NO
PT
RO
RU
TR
ZH
© kzen.dev 2023
Πηγή
stackoverflow.com
με άδεια cc by-sa 3.0 με αναφορά