2

たとえば、開発ファイルをマスターブランチにコピーするときに、次のメッセージが頻繁に表示されます。

cp: /Users/Masi/gitHub/shells/zsh/dvorak: No such file or directory
cp: /Users/Masi/gitHub/shells/zsh/dvorak2: No such file or directory

質問に「はい」と答えた場合に最初のコマンドが実行されるように、指定されたフォルダーの作成について質問されます。

存在しないディレクトリにファイルをコピーしようとしたときの擬似コードの試み

if no such a directory exists, then asks users about to create it:
   if yes, then mkdir directory AND run the initial command again
   else do noting

問題

  1. 警告メッセージを変更するには:「そのようなファイルまたはディレクトリはありません」コマンドを制御するファイルはどれですか。
  2. 最初のコマンドでパスをスクレイプし、ファイルなしでmkidrパスをスクレイプするには:最初のコマンドでパスをスクレイプするにはどうすればよいですか?
  3. AWKなどの選択した言語で最後からスクレイプするには: /がフィールドセパレーターである場合、パスの最後の一致をどのように削除しますか?AWKで最後から文字を削る方法がわかりません。
4

2 に答える 2

2

これは私が書いた関数で、zsh、bash、または ksh で動作します。

注: デバッグが有効になっています (コマンドを実行するのではなく、実行するコマンドをエコーし​​ます)。その行をコメントアウトすると、実際に実行されます。

注意:十分にテストされていません。

cpmdこれを使用するには、このスクリプトをinという名前のファイル/usr/local/bin(またはパスの別の場所) に配置します。これを有効にするには、シェル プロンプトから次のコマンドを入力します (または、起動スクリプトに追加します。bash の場合は、次のようになります~/.bashrc)。

source cpmd

次に、次のようなコマンドを使用してファイルをコピーできます。

cpmd carparts /home/dave/Documents/nonexistent/newdir/

ディレクトリ「存在しない」または「新しいディレクトリ」はまだ存在しません。両方のディレクトリが作成され、「carparts」という名前のファイルが「newdir」にコピーされます。

末尾にスラッシュ ("/") を含めない場合、最後の部分はファイル名として扱われ、それより前に存在しないディレクトリが作成されます。

cpmd supplies /home/dave/Documents/anothernew/consumables

ディレクトリ「anothernew」が作成され、「supplies」が新しいファイル名「consumables」でコピーされます。

宛先のすべてのディレクトリがすでに存在する場合はcpmd、通常のcpコマンドのように機能します。

function cpmd {
    # copies files and makes intermediate dest. directories if they don't exist
    # for bash, ksh or zsh
    # by Dennis Williamson - 2009-06-14
    # http://stackoverflow.com/questions/993266/unable-to-make-nosuchdirectory-message-useful-in-zsh

    # WARNING: no validation is performed on $1 and $2

    # all cp commands below are hardcoded with -i (interactive) to prevent overwriting

   if [[ -n $KSH_VERSION ]]
   then
       alias local=typeset
       local func="$0"
       local lastchar="${2: -1}"
       readcmd () { read "$2?$1"; }
    elif [[ -n $ZSH_VERSION ]]
    then
        local func="$0"
        # the following two lines are split up instead of doing "${2[-1]}"
        # to keep ksh from complaining when the function is loaded
        local dest="$2"
        local lastchar="${dest[-1]}"
        readcmd () { read "$2?$1"; }
    elif [[ -n $BASH_VERSION ]]
    then
    local func="$FUNCNAME"
        local lastchar="${2:(-1)}"
        readcmd () { read -p "$1" $2; }
    else
        echo "cpmd has only been tested in bash, ksh and zsh." >&2
        return 1
    fi

    local DEBUG='echo' # COMMENT THIS OUT to make this function actually work

    if [[ ${#@} != 2 ]]
    then
        echo "$func: invalid number of parameters
Usage:
  $func source destination

  where 'destination' can include nonexistent directories (which will
  be created). You must end 'destination' with a / in order for it to
  specify only directories. Without the final slash, the 'source' will
  be copied with a new name (the last portion of 'destination'). If you
  are copying multiple files and 'destination' is not a directory, the
  copy will fail." >&2
        return 1
    fi

    local dir=$(dirname "$2")
    local response
    local nl=$'\n'

    # destination ($2) is presumed to be in one of the following formats:
    # .../existdir              test 1  (-d "$2")
    # .../existdir/existfile    test 2  (-f "$2")
    # .../existdir/newfile      test 3  (-d "$dir" && $lastchar != '/')
    # .../existdir/newdir/      (else)
    # .../newdir/newdir/        (else)
    # .../newdir/newfile        (else)

    if [[ -d "$2" || -f "$2" || (-d "$dir" && $lastchar != '/') ]]
    then
        $DEBUG cp -i "$1" "$2"
    else
        if [[ $lastchar == '/' ]]
        then
            dir="$2"
        fi
        local prompt="$func: The destination directory...${nl}  ${dir}${nl}...does not exist. Create? (y/n): "
        while [[ -z $response ]]
        do
            readcmd "$prompt" response
            case $response in
                y|Y) response="Y" ;;
                n|N) ;;
                *) response=
                   prompt="$func: Invalid response.${nl}  Create destination directory? (y/n): ";;
            esac
        done
        if [[ $response == "Y" ]]
        then
            $DEBUG mkdir -p "$dir" && $DEBUG cp -i "$1" "$2"
        else
            echo "$func: Cancelled." >&2
        fi
    fi
}
于 2009-06-15T06:29:46.117 に答える
1

cpそのエラー メッセージは、zsh ではなく、コマンドから来ています。出力を改善したい場合は、パスが存在するかどうかを確認するとともに、パスを切り捨てて調べるためのロジックを記述する必要があります。

これを支援するコマンドがあります。basename(1) と dirname(1) を見てください。

于 2009-06-14T19:13:54.267 に答える