![파일 이름에 '가 있어서 (우리는) 내 스크립트를 망쳤습니다.](https://linux55.com/image/180161/%ED%8C%8C%EC%9D%BC%20%EC%9D%B4%EB%A6%84%EC%97%90%20'%EA%B0%80%20%EC%9E%88%EC%96%B4%EC%84%9C%20(%EC%9A%B0%EB%A6%AC%EB%8A%94)%20%EB%82%B4%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%A5%BC%20%EB%A7%9D%EC%B3%A4%EC%8A%B5%EB%8B%88%EB%8B%A4..png)
먼저 선택한 파일을 임시 디렉터리로 이동하고, 디렉터리를 검색하고, 가장 큰 파일을 가져와 해당 이름의 새 디렉터리를 만든 다음, 모든 파일을 새로 만든 디렉터리로 이동하는 스크립트가 있습니다.
그러나 '라는 단어와 같이 ' 문자가 포함된 디렉토리나 파일을 발견하면예 나는 이것이 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"