0

I have made a bash script that creates some simple macOS ISO images and they were all originally working. Now for some reason none of my echo commands are working, however, weirdly if I type the command manually in Mac OS's Terminal or run the script with the re-written command it works perfectly fine. The file editor I am using is Microsoft’s Visual Studio Code.

Result of working command

### Creating OS X Yosemite ISO ###

Result of non working command

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

Below I have placed the working command below the non working command (the one that Is commented out) and they look identical. Of course only the bottom one works. It’s like there is an invalid character somewhere. Now if I had the time I would write the entire thing out from scratch but that’s completely unproductive.

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

Any idea what’s going on here?

Full script

#!/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' ###"

UPDATE 1

There was no -A parameter for cat so I pulled up the manual with 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.

Result of 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' ###"

UPDATE 2

I have ran dos2unix and converted the script to a new file, ran the script and sadly it's the same result.

UPDATE 3

Result of 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|

UPDATE 4 (FIXED)

I can definetly see the differnce between the two echo commands when inspected with a hex dump. My question now is, how do I remove them in bulk?

The nano and vi CLI editors don't show any weird characters and I even installed the Atom editor in case Visual Studio Code was playing up and that doesn't show anything out of the blue.

In Atom and Visual Studio Code both editors are set to UTF-8 encoding. However, it's not until I set the encoding to DOS (CP 347) where I start seeing weird characters in commands I know that are causing issues. This is the script with DOS (CP 347) encoding enabled:

#!/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' ###"

Is dos2unix supposed to fix this because I didn't have any luck when I ran it using dos2unix -n /Users/will/Desktop/yosemite.sh output.sh?

I used the 'Find & replace' feature in Visual Studio Code and replaced all   with a space, saved it, changed the encoding back to UFT-8, re-ran the script and it works.

10
  • If you are modifying a file that is to be used on a Linux/Unix-like OS using an editor on a Windows machine, be aware that Windows uses different line-ending-indicators than Unix. You may have to run the file through dos2unix in order to get that right.
    – AdminBee
    Feb 26, 2021 at 13:39
  • I can reproduce your result if I put the double quote before the echo instead of before the first ###. Is it possible that you have a quote before the echo? Feb 26, 2021 at 13:43
  • 6
    Is it possible that you edited the script in a word processor program or "smart" editor that has replaced the space characters with something like unicode non-breaking spaces? Try examining it with cat -A for example. Feb 26, 2021 at 13:47
  • 3
    Can you add the output of hexdump -C SCRIPTNAME | head if you have hexdump, or od -c SCRIPTNAME | head if you don't to the question. This is aiming to get the same information as the cat -A but with more precision.
    – icarus
    Feb 26, 2021 at 16:53
  • 3
    The hexdump shows you do have non-breaking spaces instead of plain spaces. E.g. in "echo..-e.."###", the ".." are pairs of non-ASCII bytes, and if you look at the hex section, those correspond to "c2 a0" which is a non-breaking space (in UTF-8 encoding). Feb 26, 2021 at 18:09

2 Answers 2

1

Take the following line as an example :

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 is a space 20 with the high bit set ?? C2 is 42 with the high bit set ?

Get rid of that weird stuff. Use vi

Update 1: to create a new file

Select (use the mouse) and then copy using CTRL-C (or on mac cmd-C) your original script from your question on this webpage and create a new file :

$ cat > fixscript.sh

Now paste the contents into cat and use CTRL-D to terminate the input. Here's the output from hd fixedscript.sh of the same line above.

00000010  72 0a 65 63 68 6f 20 2d  65 20 22 23 23 23 20 43  |r.echo -e "### C|
1
  • 2
    C2A0 is the UTF-8 encoding of Unicode character 00A0 (no-break space, a.k.a. non-breaking space)
    – xhienne
    Feb 26, 2021 at 19:11
0

The standard answer to editing binary files in vim is to use xxd to convert the file to hex, make the change, and then use xxd -r to convert it back to binary.

However for this I would probably reach for perl.

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

There is also a -i flag to do in-place editing when you are confident.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .