効率的なzsh関数の作成

効率的なzsh関数の作成

私は完全に混乱していることを認めなければならない。私は数時間努力して検索しましたが、役に立ちませんでした。私はzshを私の個人的な機能と調和させることはできません。

$ print -l $fpath
/home/terry/.zsh/functions

デフォルト値と同様に、ここでは必要ありません。

ここに機能を配置することを選択しました。だから私たちはここで大丈夫です。

/home/terry/.zsh/functions次のファイルが含まれています。

-rwxrw-r-- 1 terry terry  274 Feb  6 19:20 _all-files-to-top
-rwxrw-r-- 1 terry terry  253 Feb  6 18:30 _check-for-empty-files
-rwxrw-r-- 1 terry terry  452 Feb  6 18:32 _check-video-duration
-rwxrw-r-- 1 terry terry  302 Feb  6 18:33 _error-1
-rwxrw-r-- 1 terry terry  462 Feb  6 18:34 _error-2
-rwxrw-r-- 1 terry terry  198 Feb  6 18:34 _just-hevc
-rwxrw-r-- 1 terry terry  188 Feb  6 18:36 _just-volume

指示に従ってファイルの前にアンダースコアを追加しました。

リストの一番上から1つを選択してください。

#compdef all-files-to-top
#
_all-files-to-top() {
mkdir duplicate_files
mkdir ~/Replace
#
find . -type f -exec mv -n {} ./ \;
find . -empty -type d -delete
while [ -d * ]; do
array=(find . -type d)
for x in $array ; do mv -t duplicate_files "$x" ; done
find . -name "duplicate_files" -exec mv {} ~Replace
return
}

私はそれを持っていることなしにそれを試しました#compdef
私はそれを持っている#!/bin/zsh
ことなしに#compdef
それを試しました#!/bin/zsh

root以外のユーザーがファイルを実行可能にするかどうかに関する情報が見つからなかったため、実行可能ファイルと実行不可能なファイルを試しました。

また、_all-files-to-top() { を使用するか、使用せず、アンダースコアを使用または使用しないで試しました。

この関数で始まるプログラムを呼び出し、#!/bin/zsh -xv デバッグのためにターミナルコマンドラインから直接起動してください。

画面出力です

terry-TP500LA% 2mkv
# /etc/zsh/zshenv: system-wide .zshenv file for zsh(1).
#
# This file is sourced on all invocations of the shell.
# If the -f flag is present or if the NO_RCS option is
# set within this file, all other initialization files
# are skipped.
#
# This file should contain commands to set the command
# search path, plus other important environment variables.
# This file should not contain commands that produce
# output or assume the shell is attached to a tty.
#
# Global Order: zshenv, zprofile, zshrc, zlogin

if [[ -z "$PATH" || "$PATH" == "/bin:/usr/bin" ]]
then
    export PATH="/usr/local/bin:/usr/bin:/bin:/usr/games"
fi
+/etc/zsh/zshenv:15> [[ -z /home/terry/scripts/cron:/home/terry/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin || /home/terry/scripts/cron:/home/terry/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin == /bin:/usr/bin ]]
#!/bin/zsh -xv

top=$(pwd)
+/home/terry/scripts/2mkv:3> top=+/home/terry/scripts/2mkv:3> pwd
+/home/terry/scripts/2mkv:3> top=/home/terry/New_Videos 


# call function
# moves any duplicate files to ~/Replace
mkdir duplicate_files
+/home/terry/scripts/2mkv:8> mkdir duplicate_files
mkdir ~/Replace
+/home/terry/scripts/2mkv:9> mkdir /home/terry/Replace
mkdir: cannot create directory ‘/home/terry/Replace’: File exists
all-files-to-top
+/home/terry/scripts/2mkv:10> all-files-to-top
/home/terry/scripts/2mkv:10: command not found: all-files-to-top

exit 0
+/home/terry/scripts/2mkv:12> exit 0
terry-TP500LA% 

ああ、そして最後に一つ - スペルです。名前をクリップボードにコピーし、各インスタンスで検索と置換を使用しました。

私は何をもっと試すべきかは本当にわかりません。どのフレンドリーな人がfooを持たない関数の実際の例を提供し、どのように機能するかを説明できますか?スクリプトや関数で使用されるエンコード方式と似ていませんか?私はUTF-8です。

all-files-to-top
+/home/terry/scripts/2mkv:10> all-files-to-top
/home/terry/scripts/2mkv:10: command not found: all-files-to-top

ベストアンサー1

これを機能させるには、次の2つのことを行う必要があります。

  1. zsh関数を含むスクリプトをロードする必要があることを知らせる必要があります。これは、次のコマンドを使用して実行できますautoload

     autoload -Uz FILENAME
    

    FILENAMEあなたの例_all-files-to-top

  2. 定義された関数名で関数を呼び出す:all-files-to-top関数名が呼び出されません_all-files-to-top

    先頭_は自動的に削除されず、特別な内容もありません。慣例的に、完成関数(完成を生成するために押されたときに呼び出される関数)は、コマンドを中断することなく意味のある名前を持つことができるようTabに始まる必要があります。_これは完成関数のようには見えないので、この規則に従う理由はありません。 (また#compdef、この場合は `は必要ありません。)

おすすめ記事