2016-04-13 11 views
0

ファイル内のエイリアス命令で動作することがわかりました.bashrc。 ただし、シェルスクリプトln_hookエイリアスを別のシェルスクリプトに伝播させる方法

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

と、別のシェルスクリプト:次に

#!/bin/sh 
#filename: test_ln 

type ln 

. ./ln_hookを実行し、出力:

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

は作るためにそこの回避策ですエイリアスは他のスクリプトを実行するときに有効ですか?

+0

エイリアスは、一般的に非対話型のコンテキストでは使用できません。そのための関数を使用します。あなたの関数が再帰的になるのを避けるには、 'command ln'を使います。 –

+0

こんにちは〜もしそうなら、この問題の代替ソリューションがありますか? – samm

+0

はい、man bash: 組み込みシェル組み込み[arguments] 組み込みの指定シェルを実行し、引数を渡して終了ステータスを返します。これは、名前がシェル組み込み関数と同じである関数 を定義し、組み込み関数の機能をその関数内に保持する場合に便利です。このCD組み込み関数は、このように再定義されています。シェル組み込みコマンドがシェル組み込みコマンドでない場合、戻り状況はfalseです。 – samm

答えて

0

Bashマニュアルとgoogleの後、export -f function_nameが欲しいものです。関数は再帰的であることについてのアドバイスやフォローなどのデモコードの

感謝Etan Reisner:ここ

#!/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 是函数...' 

DemoまたはBash shell editor and execute online

関連する問題