내 코드가 작동합니다. 그것은 내가 원하는 것을하고있다. 기본적으로 내 스크립트는 파일이 배치 된 마지막 두 디렉토리와 일치하도록 파일 이름을 바꾼 다음 0을 채 웁니다. 또한 디렉토리에 입력하면 지정된 디렉토리의 파일을 변경하는 인수를 취합니다.배치 파일의 이름을 바꾸지 만 파일 이름에 공백이 있으면 오류가 발생합니다. 수정이 필요합니다! Bash
여기 내 코드입니다 :
r="[email protected]"
if [ -d "$r" ]; then # if string exists and is a directory then do the following commands
cd "$r" # change directory to the specified name
echo "$r" # print directory name
elif [ -z "$1" ]; then # if string argument is null then do following command
echo "Current Directory" # Print Current Directory
else # if string is not a directory or null then do nothing
echo "No such Directory" # print No such Directory
fi
e=`pwd | awk -F/ '{ print $(NF-1) "_" $NF }'` # print current directory | print only the last two fields
echo $e
X=1;
for i in `ls -1`; do # loop. rename all files in directory to "$e" with 4 zeroes padding.
mv $i $e.$(printf %04d.%s ${X%.*} ${i##*.}) # only .jpg files for now, but can be changed to all files.
let X="$X+1"
done
그리고 여기에 출력됩니다 : 파일 이름이 그들에 공백이있을 때
Testdir_pics.0001.jpg
Testdir_pics.0002.jpg
...
그러나, 제목이 시사하는 것처럼, 그것은 오류를 생성합니다. 이 문제를 어떻게 해결할 수 있습니까? 파일 이름에 공백이있는 경우
OSX, Bash, Unix 및 Batch (Windows)로 질문에 태그를 추가했습니다. 어떤 시스템이 실제로 켜져 있습니까 ?? –
[SpellCheck] (http://www.shellcheck.net/)을 통해 스크립트를 실행하십시오. – BroSlow
죄송합니다. Mac OSX에서 터미널을 사용하고 있습니다. –