2016-04-13 5 views
0

Ich weiß, dass es für Alias-Anweisungen in der Datei .bashrc funktioniert. jedoch ein Shell-Skript ln_hook:Wie ein Alias ​​in ein anderes Shell-Skript übertragen werden kann

#!/bin/sh 
#filename: ln_hook 

## http://stackoverflow.com/questions/3648819/how-to-make-symbolic-link-with-cygwin-in-windows-7 
## https://cygwin.com/cygwin-ug-net/using-cygwinenv.html 
## https://cygwin.com/cygwin-ug-net/using.html#pathnames-symlinks 
# export CYGWIN="winsymlinks" # ln -s: The shortcut style symlinks with file extension '.lnk' 
# export CYGWIN="winsymlinks:native" # ln -s: plain text file 

ln_hook(){ 
    if [[ "-s" == "$1" ]]; then 
     cmd /C mklink /H "$(cygpath -aw "$3")" "`cygpath -aw "$2"`" 
    else 
     echo -e "\033[32m >> $* \033[0m" 
     ln "$*" 
    fi 
} 
alias ln='ln_hook' 
type ln 

(type ln) 


# cannot propagate to another shell script 

./test_ln 

$(dirname $0)/test_ln 

## . ./ln_hook 
## type ln 

und anderen Shell-Skript:

#!/bin/sh 
#filename: test_ln 

type ln 

Dann . ./ln_hook läuft, Ausgang:

ln_hook 
ln 是 `ln_hook' 的别名 
ln 是 `ln_hook' 的别名 
ln 是 /usr/bin/ln 
ln 是 /usr/bin/ln 

Gibt es eine Abhilfe die machen Alias ​​effektiv beim Ausführen anderer Skripts?

+0

Aliases sind in der Regel nicht im nicht-interaktiven Kontexten. Verwenden Sie Funktionen dafür. Verwenden Sie 'command ln', um zu vermeiden, dass Ihre Funktion rekursiv ist. –

+0

Hi ~ Wenn ja, gibt es eine alternative Lösung für dieses Problem? – samm

+0

Ja, man bash: builtin shell-butin [arguments] Führt die angegebene Shell builtin aus, übergibt ihr Argumente und gibt ihren Exit-Status zurück. Dies ist nützlich, wenn Sie eine Funktion definieren, deren Name mit einer eingebauten Shell übereinstimmt, wobei die Funktionalität der in der Funktion eingebauten Funktion erhalten bleibt. Die eingebaute CD ist auf diese Weise neu definiert. Der Rückgabestatus ist falsch, wenn Shell-Built-In kein Shell-Built-In-Befehl ist. – samm

Antwort

0

Nach dem Bash Handbuch und google, export -f function_name ist was ich will.

Thank Etan Reisner für die Beratung über Funktion rekursiven und einem Demo-Code wie folgt sein:

#!/bin/bash --posix 
# Avoid an error about function xx() statement on some os, e.g. on ubuntu, Syntax error: "(" unexpected 
# http://ubuntuforums.org/archive/index.php/t-499045.html 

######################################################################################################### 
# << A example about exporting a function >> 
# hook ln and propagate it to other scripts to pollute the environment of subsequently executed commands 
######################################################################################################### 

## https://stackoverflow.com/questions/3648819/how-to-make-symbolic-link-with-cygwin-in-windows-7 
## https://cygwin.com/cygwin-ug-net/using-cygwinenv.html 
## https://cygwin.com/cygwin-ug-net/using.html#pathnames-symlinks 
# export CYGWIN="winsymlinks" # ln -s: The shortcut style symlinks with file extension '.lnk' 
# export CYGWIN="winsymlinks:native" # ln -s: plain text file 

## ln_hook 
function ln(){ 
    if [[ "-s" == "$1" ]]; then 
     cmd /C mklink /H "$(cygpath -aw "$3")" "`cygpath -aw "$2"`" 
    else 
     echo -e "\033[32m >>ln $* \033[0m" 
     command ln "$*" 
    fi 
} 

## Cannot propagate alias to other scripts 
## alias ln='ln_hook' 

## export -f ln=ln_hook ## ln_hook.sh: 第 23 行:export: ln=ln_hook: 不是函数 

## http://docstore.mik.ua/orelly/unix3/upt/ch29_13.htm 
## https://stackoverflow.com/questions/1885871/exporting-a-function-in-shell 
export -f ln 

echo&&echo "[^-^] After trying ln_hook" 

echo -n "main shell: " && type ln 

echo -n "subshell: " && (type ln) 


echo&&echo "[^-^] Run an external script" 

echo 'type ln' > test_ln.sh 
./test_ln.sh 
# $(dirname $0)/test_ln.sh 


## . ./ln_hook 
echo 'You can try: `type ln`, which will output: ln 是函数...' 

Hier Demo oder Bash shell editor and execute online

Verwandte Themen