ファイルまたはディレクトリをソースディレクトリからコピー先ディレクトリに移動し、シンボリックリンクをソースディレクトリに置くbashスクリプトを作成しようとしています。Bash:ファイル/ディレクトリを移動してリンクを作成する
したがって、<source_path>
はファイルまたはディレクトリです。<destination_dir_path>
は元のファイルを移動したいディレクトリです。
使用例:
$ mvln /source_dir/file.txt /destination_dir/
OR
$ mvln /source_dir/dir_I_want_to_move/ /destination_dir/
これは、私が一緒に入れて管理しているものですが、それが正常に動作しません。
mv: unable to rename `/source_dir/some_file.txt': Not a directory
とディレクトリをdestination_directoryとに移動されていないだけで、その内容が移動されています それはエラーを返しますそれ以外の場合は、MV、ソースがディレクトリである場合にのみ機能します。
#!/bin/bash
SCRIPT_NAME='mvln'
USAGE_STRING='usage: '$SCRIPT_NAME' <source_path> <destination_dir_path>'
# Show usage and exit with status
show_usage_and_exit() {
echo $USAGE_STRING
exit 1
}
# ERROR file does not exist
no_file() {
echo $SCRIPT_NAME': '$1': No such file or directory'
exit 2
}
# Check syntax
if [ $# -ne 2 ]; then
show_usage_and_exit
fi
# Check file existence
if [ ! -e "$1" ]; then
no_file $1
fi
# Get paths
source_path=$1
destination_path=$2
# Check that destination ends with a slash
[[ $destination_path != */ ]] && destination_path="$destination_path"/
# Move source
mv "$source_path" "$destination_path"
# Get original path
original_path=$destination_path$(basename $source_path)
# Create symlink in source dir
ln -s "$original_path" "${source_path%/}"
何人かお手伝いできますか?
「正しく動作しません」と定義します。 –
はい、質問を編集しました。ありがとうございました。 – Reggian
これまでスクリプトをテストしたことがうまくいっているようです。 – Sujoy