나는 다음과 같은 스크립트를 만들었습니다 rename_2_files.sh
.
#!/bin/sh
#_start
while getopts o: flag
do
case "${flag}" in
o) OPTION=${OPTARG};;
esac
done
# declare variable
RENAME ="rename"
DEFAULT ="default"
# show all ExportP* files
ls '/home/dev/Documents/Work/info/Target_script/first'*
# rename file
if [["$OPTION" == "$RENAME"]];
then
mv '/home/dev/Documents/Work/info/Target_script/first1.txt' '/home/dev/Documents/Work/info/Target_script/first1_ori.txt'
mv '/home/dev/Documents/Work/info/Target_script/first2_SL.txt' '/home/dev/Documents/Work/info/Target_script/first2.txt'
fi
# show new name o files
ls '/home/dev/Documents/Work/info/Target_script/first'*
#_end
나는 그것을 사용하여 실행
sh rename_2_files.sh -o rename
회신하다:
rename_2_files.sh: 10: rename_2_files.sh: RENAME: not found
rename_2_files.sh: 11: rename_2_files.sh: DEFAULT: not found
/home/dev/Documents/Work/info/Target_script/first1.txt
/home/dev/Documents/Work/info/Target_script/first2_SL.txt
rename_2_files.sh: 20: rename_2_files.sh: [[Rename: not found
/home/dev/Documents/Work/info/Target_script/first1.txt
/home/dev/Documents/Work/info/Target_script/first2_SL.txt
이와 관련하여 어떤 문제점이 있습니까? 도와주세요...
답변1
내 스크립트에는 구문 오류가 많이 있지만 https://www.shellcheck.net/을 사용하여 이를 수정합니다.
이제 내 스크립트('rename_2_files.sh')는 다음과 같습니다.
#!/bin/sh
#_start
while getopts o: flag
do
case "${flag}" in
o) OPTION=${OPTARG};;
*)
esac
done
# declare variable
RENAME="rename"
DEFAULT="default"
# show all ExportP* files
ls '/home/dev/Documents/Work/info/Target_script/first'*
# rename file
if [ "$OPTION" = "$RENAME" ]; then
mv '/home/dev/Documents/Work/info/Target_script/first1.txt' '/home/dev/Documents/Work/info/Target_script/first1_ori.txt'
mv '/home/dev/Documents/Work/info/Target_script/first2_SL.txt' '/home/dev/Documents/Work/info/Target_script/first2.txt'
elif [ "$OPTION" = "$DEFAULT" ]; then
mv '/home/dev/Documents/Work/info/Target_script/first1_ori.txt' '/home/dev/Documents/Work/info/Target_script/first1.txt'
mv '/home/dev/Documents/Work/info/Target_script/first2.txt' '/home/dev/Documents/Work/info/Target_script/first2_SL.txt'
fi
# show new name o files
ls '/home/dev/Documents/Work/info/Target_script/first'*
#_end
그리고 내가 사용하는 스크립트의 실행을 호출하려면:
sh rename_2_files.sh -o rename
또는
sh rename_2_files.sh -o default
모두 좋은 하루 보내시고 큰 성공을 거두시기를 바랍니다!