Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hernandito

macrumors newbie
Original poster
Sep 28, 2009
5
0
Hi Guys,

I need some help. I am trying to create a shell script of commands I typically enter in Terminal, but I am having problems. Even the most basic command like 'clear' shows up in terminal as "Command not found".

An explanation, if I enter ~/USBDisk... this is meant to go in the current user's home folder in a sub-folder called USBDisk... but the script return "no such file or directory." The one where I am copying from /Volumes/USBDisk/.... this is always the name of the volume from where I want to copy from. But the the destination's username home folder can vary.

I am also trying to save a variable "diskno" but it is not saving correctly. It saves the string I wrote, and not the result of the diskutil command I typed. When I get rid of the ' at beginning and end, I get again a "command not found error".

Any help or advice is greatly appreciated.

Code:
#!/bin/bash 

cd
mkdir USBDisk USBDisk/Backup USBDisk/Work USBDisk/Preboot

clear

#open -a Preview /Volumes/USBDisk/usr/listdisks.png


diskno='diskutil list |grep "USBDisk" |grep -o '\(disk[0-9]\)''


echo "We are backing up your USB Drive $diskno "

sudo dd if=/dev/r"$diskno" of=~/USBDisk/Backup/stick-back.img bs=51200

clear
echo Backup complete. About to start building Boot DVD image.

echo "Copying files to Work folder" 
cp /Volumes/USBDisk/usr/bootcd/cdboot ~/USBDisk/Work/
cp /Volumes/USBDisk/usr/bootcd/106/Extra ~/USBDisk/Work/
cp /Volumes/USBDisk/Extra/com.apple.Boot.plist ~/USBDisk/Work/Extra/

echo "Building Preboot.dmg"
cp /Volumes/USBDisk/Extra/com.apple.Boot.plist ~/USBDisk/Preboot/Extra/
cp /Volumes/USBDisk/Extra/dsdt.aml ~/USBDisk/Preboot/Extra/
cp /Volumes/USBDisk/Extra/SMBIOS.plist ~/USBDisk/Preboot/Extra/
cp /Volumes/USBDisk/Extra/10.6/Extensions.mkext ~/USBDisk/Preboot/Extra/
cp /Volumes/USBDisk/usr/bootcd/106/mach_kernel ~/USBDisk/Preboot/Extra/
cp /Volumes/USBDisk/usr/bootcd/106/Extra/Themes ~/USBDisk/Preboot/Extra/

echo "Creating .DMG file"
hdiutil create ~/USBDisk/Work/Prebootraw.dmg -srcfolder ~/USBDisk/Preboot
hdiutil convert ~/USBDisk/Work/Prebootraw.dmg -format UDRO -o ~/USBDisk/Work/Preboot.dmg
rm ~/USBDisk/Work/Prebootraw.dmg

echo "Copy backup image to Work Folder"
cp /Volumes/USBDisk/usr/bootcd/Restore_Device ~/USBDisk/Work/
cp ~/USBDisk/Backup/stick-back.img ~/USBDisk/Work/Restore_Device/

echo "Create ISO file"
cd 
cd ~/USBDisk/Work/
sudo hdiutil makehybrid -o ~/Desktop/BurnMe.iso ~/USBDisk/Work/ -iso -hfs -joliet -eltorito-boot ~/USBDisk/Work/cdboot no-emul-boot -hfs-volume-name "Boot-DVD" -joliet-volume-name "Boot-DVD"

echo "ISO file created, process completed. "
 
I am also trying to save a variable "diskno" but it is not saving correctly. It saves the string I wrote, and not the result of the diskutil command I typed. When I get rid of the ' at beginning and end, I get again a "command not found error".
You have the wrong kind of quotes around the diskutil command. You have single-quotes and it needs to be "back quotes" or grave accent, like this:

Code:
diskno=`diskutil list |grep "USBDisk" |grep -o '\(disk[0-9]\)'`
They may look very similar, but they are completely different. You should copy and paste the above command-line, then view it in a large point-size in a text-editor to see the difference.

As to the other problems, your use of single-quotes was creating a malformed command-line that bash can't parse. As a result, all the rest of your shell script could be misinterpreted. Since you didn't say which of the two 'clear' commands was failing, I can't be any more specific.
 
You have the wrong kind of quotes around the diskutil command. You have single-quotes and it needs to be "back quotes" or grave accent, like this:

Code:
diskno=`diskutil list |grep "USBDisk" |grep -o '\(disk[0-9]\)'`
They may look very similar, but they are completely different. You should copy and paste the above command-line, then view it in a large point-size in a text-editor to see the difference.

As to the other problems, your use of single-quotes was creating a malformed command-line that bash can't parse. As a result, all the rest of your shell script could be misinterpreted. Since you didn't say which of the two 'clear' commands was failing, I can't be any more specific.

Thank you Chown.... this was helpful, yet I am not there yet.

I decided to take smaller steps to solve a few steps at a time. I am trying this code:

Code:
#!/bin/bash
mkdir USBDrive USBDrive/Backup USBDrive/Work USBDrive/Preboot USBDrive/Preboot/Extra
clear
#open -a Preview /Volumes/USBDrive/usr/listdisks.png
diskno=`diskutil list |grep "USBDrive" |grep -o '\(disk[0-9]\)'`
echo " This is it: $diskno"
sudo dd if=/dev/r"$diskno " of=/stick-back.img bs=51200
cp /Volumes/USBDrive/usr/5.png ~/USBDrive/Work/

Line by line:

The folders I asked to create are created, or it reports they exist. This is great.

On the "clear" command, I get a "command not found".

On the echo command, I do get the disk# reported properly!Good news!

On the dd command, I get "dd; /dev/rdisk1\r : no such file or directory. When I manually typed the disk number, it did work correctly. I am missing something here.

On the cp command, it did copy the test file.

Thanks again!
 
On the "clear" command, I get a "command not found".

...

On the dd command, I get "dd; /dev/rdisk1\r : no such file or directory. When I manually typed the disk number, it did work correctly. I am missing something here.

The dd error shows a \r as part of the device pathname. A \r is a Carriage Return (CR) character. I suspect that your text file contains inconsistent line-endings, and some of them are CR or CR-LF. They must all be LF-only (Unix-style line-endings).

The easiest way to change line-endings is to use a text-editor app that can change them. Xcode's text editor can do this. So can editors like TextWrangler. The menu command in the editor may be called Normalize Line Endings, or it may be as in Xcode: under the Format menu.

Fix the line-endings and see if the problems go away. The 'clear' problem is likely coming from the fact that 'clear\r' is the command being searched for, and it doesn't exist.

You can also use the hexdump command to dump your script file in hex. CR is 0D, LF is 0A.
 
Fix the line-endings and see if the problems go away. The 'clear' problem is likely coming from the fact that 'clear\r' is the command being searched for, and it doesn't exist.

You can also use the hexdump command to dump your script file in hex. CR is 0D, LF is 0A.

That was IT!! Thank you.... I thought all text editors were the same, and I started this script in Windows... live and learn. Thank you so much... It is running through everything without problems.

I really appreciate your help.

Thanks again!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.