ファイル名に 'があり(私たちは)私のスクリプトを台無しにしました。

ファイル名に 'があり(私たちは)私のスクリプトを台無しにしました。

まず、選択したファイルを一時ディレクトリに移動し、ディレクトリを検索し、最大のファイルをインポートしてその名前の新しいディレクトリを作成し、すべてのファイルを新しく作成したディレクトリに移動するスクリプトがあります。

しかし、「という単語のように」文字を含むディレクトリまたはファイルを見つけた場合はい 私はこれがjfilenameallが正しく「引用」されていないことに関連している可能性があると思います。いくつかの方法でテストしましたが、まだ成功していません。

私が間違っていることを知っている人はいますか?

話す価値があるはい、nemo操作でこのスクリプトを実行しているため、次のコマンドラインを実行します。 script.sh "path/filename1.txt" "path/filename2.txt" ..GUIで選択したファイル数に応じて等。

jdir2="$1"
jdirfirst="${jdir2%/*}"
jdir="$jdirfirst"/
jdir0="$jdirfirst"
tmpdir="$jdir0/tmp.tmp3"
mkdir "$tmpdir"
mv "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$tmpdir"/
                
 jfilenameall=$(basename $(find $tmpdir -maxdepth 1 -type f  -printf "%s\t%p\n" | sort -n | tail -1 | awk '{print $NF}'))
jfilename="${jfilenameall::-4}"
jfilenameextension="$(echo -n $jfilenameall | tail -c 3)"
jfilename=${jdirlast::-4}    
mkdir "$jdir0/$jfilename"
mv "$jdir0/tmp.tmp3/"* "$jdir0/$jfilename/"

ベストアンサー1

これはあなたの質問に記載されているように行われます(不要に思われるいくつかの手順を削除しました)。これはGNUツールを使用すると仮定します。主な問題は、変数とコマンドの置換に二重引用符がないことです。私はまた、改行文字を含めたり、次に始まるファイル名など、もう少し奇妙なファイル名についてもこれを行いました-

#!/bin/bash

## You don't define $jdir0 in your script, so I am assuming you
## do so earlier. I'm setting it to '/tmp' here.
jdir0="/tmp"

## Create a temp dir in $jdir0
tmpdir=$(mktemp -dp "$jdir0")

## Move all arguments passed to the script to the tmp dir,
## this way you don't need to specify $1, $2 etc. The -- ensures
## this will work even if your file names start with '-'.
mv -- "$@" "$tmpdir"/


## Get the largest file's name. Assume GNU find; deal with arbitrary file names,
## including those with spaces, quotes, globbing characters or newlines
IFS= read -r -d '' jfilenameall < <(find "$tmpdir" -maxdepth 1 -type f \
                                        -printf '%s\t%p\0' | sort -zn |
                                        tail -zn1 | cut -f 2-)

## Assume GNU find; deal with arbitrary file names, including those with
## spaces, quotes, globbing characters or newlines
jfilenameall="$(basename "$jfilenameall")"

## Get the extension: whatever comes after the last . in the file name. You don't
## use this anywhere, but maybe you just don't show it. If the file has no extension,
## this variable will be set to the entire file name.
jfilenameextension="${jfilenameall##*.}"

## get the file name without the extension. Don't assume an extension will always be 3 chars
jfilename="${jfilenameall%.*}"

## You don't define $jdir0 in your script. I am assuming you do so earlier
mkdir -p "$jdir0/$jfilename"
mv -- "$tmpdir/"* "$jdir0/$jfilename/"

## remove the now empty tmp dir
rmdir "$tmpdir"

おすすめ記事