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)を使用して、このWebページの質問から元のスクリプトをコピーして新しいファイルを作成します。

$ 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|

おすすめ記事