Bash 스크립트가 손상되었습니다

Bash 스크립트가 손상되었습니다

간단한 macOS ISO 이미지를 생성하기 위해 bash 스크립트를 만들었고 처음에는 모두 작동했습니다. 이제 어떤 이유로 에코 명령이 작동하지 않지만 이상하게도 Mac OS의 터미널에 수동으로 명령을 입력하거나 재정의된 명령을 사용하여 스크립트를 실행하면 제대로 작동합니다. 제가 사용하는 파일 편집기는 Microsoft의 Visual Studio Code입니다.

작업 주문 결과

### Creating OS X Yosemite ISO ###

작동하지 않는 명령의 결과

/Users/will/Desktop/yosemite.sh: line 3: echo -e ### Creating OS X Yosemite ISO ###\n: command not found

아래에서는 작동하지 않는 명령(주석 처리된 명령) 아래에 작동하는 명령을 배치했는데 모양은 동일합니다. 물론 아래쪽만 유효합니다. 어딘가에 잘못된 문자가 있는 것 같습니다. 이제 시간이 있으면 처음부터 전체 내용을 작성하겠지만 그것은 완전히 비생산적입니다.

#echo -e "### Creating OS X Yosemite ISO ###\n"
echo -e "### Creating OS X Yosemite ISO ###\n"

무슨 일인지 아세요?

전체 스크립트

#!/bin/bash
clear
echo -e "### Creating OS X Yosemite ISO ###\n"
#echo -e "### Creating OS X Yosemite ISO ###\n"
sleep 30
echo -n "Checking if application exists... "
if [ -d "/Applications/Install OS X Yosemite.app" ] 
then
    echo "Application is already downloaded and ready to continue."
    sleep 4
    break
elif [ -f "$script_dir\Yosemite.tar.gz" ]
then
    echo -n "Extracting archive to Applications directory... "
    tar -xzvf "$script_dir\Yosemite.tar.gz" --strip-components=1 -C "/Applications/"
    echo "Done"
    sleep 4
else
    echo -e "Yosemite can not be found. \n\nPlease download the application or place the archive in the root directory with this script."
    sleep 4
    exit
fi          

echo -n "Doing clean-up... "
rm -f /tmp/Yosemite.dmg /tmp/Yosemite.sparseimage
hdiutil detach /Volumes/install_app
hdiutil detach /Volumes/install_build
echo -n "Attaching 'OS X Install ESD' disk image... "
hdiutil attach /Applications/Install\ OS\ X\ Yosemite.app/Contents/SharedSupport/InstallESD.dmg -noverify -nobrowse -mountpoint /Volumes/install_app
echo "Done"
echo -n "Converting disk image to sparse image... "
hdiutil convert /Volumes/install_app/BaseSystem.dmg -format UDSP -o /tmp/Yosemite
echo "Done"
echo -n "Attaching 'OS X Base System' image... "
hdiutil attach /tmp/Yosemite.sparseimage -noverify -nobrowse -mountpoint /Volumes/install_build
echo "Done"
echo -n "Removing redundancies... "
rm /Volumes/install_build/System/Installation/Packages
echo "Done"
echo -n "Copying files from 'OS X Install ESD' > 'OS X Base System'... "
cp -rp /Volumes/install_app/Packages /Volumes/install_build/System/Installation/
cp -rp /Volumes/install_app/BaseSystem.chunklist /Volumes/install_build
cp -rp /Volumes/install_app/BaseSystem.dmg /Volumes/install_build
echo "Done"
echo -n "Renaming volume... "
diskutil rename /Volumes/install_build Install\ OS\ X\ Yosemite
bless --folder "/Volumes/Install OS X Yosemite/System/Library/CoreServices" --label "Install OS X Yosemite"
echo "Done"
echo -n "Unmounting volumes... "
hdiutil detach /Volumes/install_app
hdiutil detach /Volumes/Install\ OS\ X\ Yosemite
echo "Done"
echo -n "Converting Sparse image to ISO file... "
hdiutil convert /tmp/Yosemite.sparseimage -format UDTO -o /tmp/Yosemite.cdr
echo "Done"
echo -n "Renaming file extension and moving ISO to current user's desktop... "
mv /tmp/Yosemite.cdr ~/Desktop/Yosemite.iso
echo "Done"
echo -n "Removing left-over files... "
rm -f /tmp/Yosemite.dmg /tmp/Yosemite.sparseimage
echo "Done"
sleep 4
echo -e "\n### Successfully created 'Install OS X Yosemite.iso' ###"

업데이트 1

-A매개변수 가 없으므로 다음을 cat사용하여 매뉴얼을 열었습니다 man cat.

     -b      Number the non-blank output lines, starting at 1.

     -e      Display non-printing characters (see the -v option), and display a dollar sign (`$') at the end of each line.

     -n      Number the output lines, starting at 1.

     -s      Squeeze multiple adjacent empty lines, causing the output to be single spaced.

     -t      Display non-printing characters (see the -v option), and display tab characters as `^I'.

     -u      Disable output buffering.

     -v      Display non-printing characters so they are visible.  Control characters print as `^X' for control-X; the delete character (octal 0177) prints as `^?'.  Non-ASCII characters (with the high bit set) are printed as `M-' (for meta)
             followed by the character for the low 7 bits.

결과cat -etv /Users/musicroom/Desktop/yosemite.sh

#!/bin/bash$
clear$
echo -e "### Creating OS X Yosemite ISO ###\n"$
#echo -e "### Creating OS X Yosemite ISO ###\n"$
sleep 30$
echo -n "Checking if application exists... "$
if [ -d "/Applications/Install OS X Yosemite.app" ] $
then$
    echo "Application is already downloaded and ready to continue."$
    sleep 4$
    break$
elif [ -f "$script_dir\Yosemite.tar.gz" ]$
then$
    echo -n "Extracting archive to Applications directory... "$
    tar -xzvf "$script_dir\Yosemite.tar.gz" --strip-components=1 -C "/Applications/"$
    echo "Done"$
    sleep 4$
else$
    echo -e "Yosemite can not be found. \n\nPlease download the application or place the archive in the root directory with this script."$
    sleep 4$
    exit$
fi^I^I^I$
$
echo -n "Doing clean-up... "$
rm -f /tmp/Yosemite.dmg /tmp/Yosemite.sparseimage$
hdiutil detach /Volumes/install_app$
hdiutil detach /Volumes/install_build$
echo -n "Attaching 'OS X Install ESD' disk image... "$
hdiutil attach /Applications/Install\ OS\ X\ Yosemite.app/Contents/SharedSupport/InstallESD.dmg -noverify -nobrowse -mountpoint /Volumes/install_app$
echo "Done"$
echo -n "Converting disk image to sparse image... "$
hdiutil convert /Volumes/install_app/BaseSystem.dmg -format UDSP -o /tmp/Yosemite$
echo "Done"$
echo -n "Attaching 'OS X Base System' image... "$
hdiutil attach /tmp/Yosemite.sparseimage -noverify -nobrowse -mountpoint /Volumes/install_build$
echo "Done"$
echo -n "Removing redundancies... "$
rm /Volumes/install_build/System/Installation/Packages$
echo "Done"$
echo -n "Copying files from 'OS X Install ESD' > 'OS X Base System'... "$
cp -rp /Volumes/install_app/Packages /Volumes/install_build/System/Installation/$
cp -rp /Volumes/install_app/BaseSystem.chunklist /Volumes/install_build$
cp -rp /Volumes/install_app/BaseSystem.dmg /Volumes/install_build$
echo "Done"$
echo -n "Renaming volume... "$
diskutil rename /Volumes/install_build Install\ OS\ X\ Yosemite$
bless --folder "/Volumes/Install OS X Yosemite/System/Library/CoreServices" --label "Install OS X Yosemite"$
echo "Done"$
echo -n "Unmounting volumes... "$
hdiutil detach /Volumes/install_app$
hdiutil detach /Volumes/Install\ OS\ X\ Yosemite$
echo "Done"$
echo -n "Converting Sparse image to ISO file... "$
hdiutil convert /tmp/Yosemite.sparseimage -format UDTO -o /tmp/Yosemite.cdr$
echo "Done"$
echo -n "Renaming file extension and moving ISO to current user's desktop... "$
mv /tmp/Yosemite.cdr ~/Desktop/Yosemite.iso$
echo "Done"$
echo -n "Removing left-over files... "$
rm -f /tmp/Yosemite.dmg /tmp/Yosemite.sparseimage$
echo "Done"$
sleep 4$
echo -e "\n### Successfully created 'Install OS X Yosemite.iso' ###"

업데이트 2

dos2unix를 실행하고 스크립트를 새 파일로 변환하고 스크립트를 실행했는데 불행히도 결과는 동일했습니다.

업데이트 3

결과hexdump -C /Users/musicroom/Desktop/yosemite.sh | head

00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0a 63 6c 65 61  |#!/bin/bash.clea|
00000010  72 0a 65 63 68 6f c2 a0  2d 65 c2 a0 22 23 23 23  |r.echo..-e.."###|
00000020  c2 a0 43 72 65 61 74 69  6e 67 c2 a0 4f 53 c2 a0  |..Creating..OS..|
00000030  58 c2 a0 59 6f 73 65 6d  69 74 65 c2 a0 49 53 4f  |X..Yosemite..ISO|
00000040  c2 a0 23 23 23 5c 6e 22  0a 23 65 63 68 6f 20 2d  |..###\n".#echo -|
00000050  65 20 22 23 23 23 20 43  72 65 61 74 69 6e 67 20  |e "### Creating |
00000060  4f 53 20 58 20 59 6f 73  65 6d 69 74 65 20 49 53  |OS X Yosemite IS|
00000070  4f 20 23 23 23 5c 6e 22  0a 73 6c 65 65 70 20 33  |O ###\n".sleep 3|
00000080  30 0a 65 63 68 6f 20 2d  6e 20 22 43 68 65 63 6b  |0.echo -n "Check|
00000090  69 6e 67 20 69 66 20 61  70 70 6c 69 63 61 74 69  |ing if applicati|

업데이트 4(수정됨)

Hex Dump를 사용하여 검사할 때 두 echo 명령의 차이점을 명확하게 볼 수 있습니다. 이제 내 질문은 대량으로 삭제하려면 어떻게 해야 합니까?입니다.

nano 및 vi CLI 편집기는 이상한 문자를 표시하지 않으며 Visual Studio Code가 실행될 때 갑자기 아무것도 표시하지 않는 경우를 대비해 Atom 편집기도 설치했습니다.

Atom 및 Visual Studio Code에서는 두 편집기 모두 UTF-8 인코딩으로 설정됩니다. 그러나 DOS (CP 347)명령에서 이상한 문자가 보이기 시작하는 인코딩을 설정하기 전까지는 이것이 문제를 일으키는지 몰랐습니다 . 인코딩을 활성화하는 스크립트는 다음과 같습니다 DOS (CP 347).

#!/bin/bash
clear
echo -e "### Creating OS X Yosemite ISO ###\n"
#echo -e "### Creating OS X Yosemite ISO ###\n"
sleep 30
echo -n "Checking if application exists... "
if [ -d "/Applications/Install OS X Yosemite.app" ] 
then
    echo "Application is already downloaded and ready to continue."
    sleep 4
    break
elif [ -f "$script_dir\Yosemite.tar.gz" ]
then
    echo -n "Extracting archive to Applications directory... "
    tar -xzvf "$script_dir\Yosemite.tar.gz" --strip-components=1 -C "/Applications/"
    echo "Done"
    sleep 4
else
    echo -e "Yosemite can not be found. \n\nPlease download the application or place the archive in the root directory with this script."
    sleep 4
    exit
fi          

echo -n "Doing clean-up... "
rm -f /tmp/Yosemite.dmg /tmp/Yosemite.sparseimage
hdiutil detach /Volumes/install_app
hdiutil detach /Volumes/install_build
echo -n "Attaching 'OS X Install ESD' disk image... "
hdiutil attach /Applications/Install\ OS\ X\ Yosemite.app/Contents/SharedSupport/InstallESD.dmg -noverify -nobrowse -mountpoint /Volumes/install_app
echo "Done"
echo -n "Converting disk image to sparse image... "
hdiutil convert /Volumes/install_app/BaseSystem.dmg -format UDSP -o /tmp/Yosemite
echo "Done"
echo -n "Attaching 'OS X Base System' image... "
hdiutil attach /tmp/Yosemite.sparseimage -noverify -nobrowse -mountpoint /Volumes/install_build
echo "Done"
echo -n "Removing redundancies... "
rm /Volumes/install_build/System/Installation/Packages
echo "Done"
echo -n "Copying files from 'OS X Install ESD' > 'OS X Base System'... "
cp -rp /Volumes/install_app/Packages /Volumes/install_build/System/Installation/
cp -rp /Volumes/install_app/BaseSystem.chunklist /Volumes/install_build
cp -rp /Volumes/install_app/BaseSystem.dmg /Volumes/install_build
echo "Done"
echo -n "Renaming volume... "
diskutil rename /Volumes/install_build Install\ OS\ X\ Yosemite
bless --folder "/Volumes/Install OS X Yosemite/System/Library/CoreServices" --label "Install OS X Yosemite"
echo "Done"
echo -n "Unmounting volumes... "
hdiutil detach /Volumes/install_app
hdiutil detach /Volumes/Install\ OS\ X\ Yosemite
echo "Done"
echo -n "Converting Sparse image to ISO file... "
hdiutil convert /tmp/Yosemite.sparseimage -format UDTO -o /tmp/Yosemite.cdr
echo "Done"
echo -n "Renaming file extension and moving ISO to current user's desktop... "
mv /tmp/Yosemite.cdr ~/Desktop/Yosemite.iso
echo "Done"
echo -n "Removing left-over files... "
rm -f /tmp/Yosemite.dmg /tmp/Yosemite.sparseimage
echo "Done"
sleep 4
echo -e "\n### Successfully created 'Install OS X Yosemite.iso' ###"

dos2unix를 사용하여 실행할 때 행운이 없기 때문에 이 문제를 해결해야 합니까 dos2unix -n /Users/will/Desktop/yosemite.sh output.sh?

Visual Studio Code의 찾기 및 바꾸기 기능을 사용하고 모든 것을 ┬á공백으로 바꾸고 저장한 다음 인코딩을 다시 UFT-8로 변경하고 스크립트를 다시 실행하면 제대로 작동했습니다.

답변1

다음 동작의 예:

00000010  72 0a 65 63 68 6f c2 a0  2d 65 c2 a0 22 23 23 23  |r.echo..-e.."###|
What are these characters -- ^  ^        ^   ^

a0은 상위 비트가 20으로 설정된 공간인가요?? C2의 상위 비트는 42로 설정되어 있나요?

이상한 것들을 제거하십시오. vi를 사용하세요

업데이트 1: 새 파일 만들기

마우스를 사용하여 선택한 다음 CTRL-C(또는 Mac에서는 cmd-C)를 사용하여 이 웹페이지의 질문에서 원본 스크립트를 복사하고 새 파일을 만듭니다.

$ cat > fixscript.sh

이제 CTRL-D를 사용하여 내용을 붙여넣고 cat입력을 종료합니다. 다음은 hd fixedscript.sh위와 같은 줄의 출력 입니다 .

00000010  72 0a 65 63 68 6f 20 2d  65 20 22 23 23 23 20 43  |r.echo -e "### C|

답변2

vim에서 바이너리 파일을 편집하는 표준적인 대답은 xxdvim을 사용하여 파일을 16진수로 변환하고 변경한 다음 xxd -rvim을 사용하여 다시 바이너리로 변환하는 것입니다.

그러나 아마도 나는 이것을 위해 Perl을 사용할 것입니다.

perl -p0e 's/\x{c2}\x{a0}/ /g' input_file > output_file

자신감이 있을 때 -i바로 편집할 수 있는 플래그 도 있습니다.

관련 정보