Building a script in Ubuntu (Linux) from existing DOS .cmd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Building a script in Ubuntu (Linux) from existing DOS .cmd
# 1  
Old 12-14-2009
Question Building a script in Ubuntu (Linux) from existing DOS .cmd

Greetings,
I would be very grateful to anyone who is willing to help and knows there scripting and how to convert a DOS CMD script. Smilie
In this posting I am also hoping to pass on information to anyone wanting to see a bit of VMware command line stuff.
I have made a script (batch file) that runs great in windows but I want to run it on Ubuntu (9.04 64-bit).
The problem is that dividual commands run independently on the command line, but in a script it doesn't. It seems like only the first command runs and the rest don't really work.
I want to know how best to create the script file and run it. I also want to learn on the way.
I don't have the time to learn more shell scripting right now and want to get the job done. I tried to get it to work but it all just behaves strangely.

I created this script to quickly deploy 3 VMware virtual machines (2 are clones from a base image and one gets unzipped). The script does fancy stuff like change the vmnets and adjust the memory and display names of the new VMs. The idea is to use them and then later redeploy them afresh.
I will use something like "/home/administrator/vmware/Virtual Machines" instead of "c:\Virtual Machines" and /usr/bin/vmware instead of vmware.exe

Here is the working DOS script and below some conversion that I have created
===================================================
Code:
@echo off
cd\
cd "Virtual Machines\VMs\"
rmdir England /S /Q
mkdir England
c:
cd\
cd "Virtual Machines\VMs\"
cls
echo.
echo Creating VMs ....
echo.
echo Start time:
echo.
time /T
time /T > timenow.txt
rem ##### PC 1
"C:\Program Files\VMware\VMware Workstation\vmrun.exe" clone "C:\Virtual Machines\VMs\XPVM\Windows XP Pro.vmx" "C:\Virtual Machines\VMs\England\pc1\pc1.vmx" linked
rem the next line looks in the vmx file for clone.... value and replaces with pc1
cscript replace.vbs "C:\Virtual Machines\VMs\England\pc1\pc1.vmx" "Clone of Windows XP Pro" "pc1" 
echo.
echo PC done.
echo.
time /T
echo.
rem ##### pc2
"C:\Program Files\VMware\VMware Workstation\vmrun.exe" clone "C:\Virtual Machines\VMs\XPVM\Windows XP Pro.vmx" "C:\Virtual Machines\VMs\England\pc2\pc2.vmx" linked
cscript replace.vbs "C:\Virtual Machines\VMs\England\pc2\pc2.vmx" "Clone of Windows XP Pro" "pc2" 
cscript replace.vbs "C:\Virtual Machines\VMs\England\pc2\pc2.vmx" "VMnet2" "VMnet3" 
cscript replace.vbs "C:\Virtual Machines\VMs\England\pc2\pc2.vmx" "512" "256" 
echo.
echo pc2 done.
echo.
time /T
echo.
7z e "C:\Virtual Machines\VMs\pc3.7z" -o"C:\Virtual Machines\VMs\England\pc3\" 
"C:\Program Files\VMware\VMware Workstation\vmware-vdiskmanager.exe" -c -s 10240MB -a lsilogic -t 2 "C:\Virtual Machines\VMs\England\pc3\pc3-000001.vmdk" 
echo.
echo pc3 done.
echo.
time /T
echo.
sleep 5
cls
echo.
echo.
rem sleep 10
cls
echo.
echo  The time now is:
time /T
echo     Launching VMware Workstation Console now ....
echo.
echo.
echo Start time was:
type timenow.txt
echo.
time /T
echo.
echo.
echo.
rem cls
echo.
echo All done !
echo.
echo Job complete
echo.
echo.
sleep 30

===================================================
END

Conversions
========


This is the base image being used to make a linked clone, to later be called pc1
/usr/bin/vmrun -T ws clone "/home/administrator/vmware/Virtual Machines/CheckPoint/XPVM/Windows XP Pro.vmx" "/home/administrator/vmware/Virtual Machines/CheckPoint/England/pc1/pc1.vmx" linked


This replaced the vb script (cscript replace (above)) for replacing text values in the file:
rpl "Clone of Windows XP Pro" "pclondon" "/home/administrator/vmware/Virtual Machines/England/pc1/pc1.vmx"

Last edited by dp123; 12-14-2009 at 04:36 PM..
# 2  
Old 12-14-2009
Quote:
Originally Posted by dp123
Greetings,
I would be very grateful to anyone who is willing to help and knows there scripting and how to convert a DOS CMD script. Smilie
In this posting I am also hoping to pass on information to anyone wanting to see a bit of VMware command line stuff.
I have made a script (batch file) that runs great in windows but I want to run it on Ubuntu (9.04 64-bit).
The problem is that dividual commands run independently on the command line, but in a script it doesn't. It seems like only the first command runs and the rest don't really work.
I want to know how best to create the script file and run it. I also want to learn on the way.
I don't have the time to learn more shell scripting right now and want to get the job done. I tried to get it to work but it all just behaves strangely.
Can you show us what you've tried? You may be halfway there already.
# 3  
Old 12-14-2009
Sure (see below). Thanks for your response.
I just recreated most of it (I hadn't saved it all and deletes the last version of file)
The main problem is that the first line or two of the script works and then each one after that doesn't. But each line pasted into a terminal works fine. I just donw want to have to run it all manually each time or run a seperate script for each line!
So I just don't know how to get the script to run each line properly as an independent command.

I get errors like this:
"errors:
Creating VMs ....
Error: Invalid clone type: must be "full" or "linked"
" not found.home/administrator/vmware/Virtual Machines/VMs/England/pc1/pc1.vmx
PC 1."

Code:
 
#!/bin/bash
rmdir -p "/home/administrator/vmware/Virtual Machines/VMs/England"
mkdir "/home/administrator/vmware/Virtual Machines/VMs/England"
REM THESE FIRST 3 LINES SEEM TO WORK
echo Creating VMs ....
echo ##### PC 1
/usr/bin/vmrun -T ws clone "/home/administrator/vmware/Virtual Machines/VMs/XPVM/Windows XP Pro.vmx" "/home/administrator/vmware/Virtual Machines/VMs/England/pc1/pc1.vmx" linked
rpl "Clone of Windows XP Pro" "pc1" "/home/administrator/vmware/Virtual Machines/VMs/England/pc1/pc1.vmx"
echo 
echo PC 1 done.
REM THESE LINES DONT WORK IN THE SCRIPT BUT DO IN THE COMMAND LINE
read -p "Press any key to continue"
rem ##### PC 2
/usr/bin/vmrun -T ws clone "/home/administrator/vmware/Virtual Machines/VMs/XPVM/Windows XP Pro.vmx" "/home/administrator/vmware/Virtual Machines/VMs/England/pc2/pc2.vmx" linked
rpl "Clone of Windows XP Pro" "pc2" "/home/administrator/vmware/Virtual Machines/VMs/England/pc2/pc2.vmx"
rpl "VMnet2" "VMnet3" "/home/administrator/vmware/Virtual Machines/VMs/England/pc2/pc2.vmx"
rpl "512" "256" "/home/administrator/vmware/Virtual Machines/VMs/England/pc2/pc2.vmx"
echo 
REM THIS ONE IS UNZIPPING A VM AND THEN CREATING THE BLANK HARD DRIVE (PRE_ALLOCATED 10GB SPACE).
echo PC 2 done.
rem ##### PC 3
7z e "/home/administrator/vmware/Virtual Machines/VMs/pc3.7z" -o"/home/administrator/vmware/Virtual Machines/VMs/England/pc3/"
/usr/bin/vmware-vdiskmanager -c -s 10240MB -a lsilogic -t 2 "/home/administrator/vmware/Virtual Machines/VMs/England/pc3/pc3-000001.vmdk"
echo


Last edited by dp123; 12-14-2009 at 04:34 PM..
# 4  
Old 12-14-2009
2 things:
  1. Shell comments don't start with REM, but with a hash '#', and can start anywhere on the line
  2. If you want to output literal hash characters, enclose them or the complete string with single or double quotes. Otherwise you'll start a comment.
# 5  
Old 12-14-2009
Some notes

The 7z command has '-o"text"', does it require a space?

You might try single quotes around the vmx files. From the first error it appears to be dropping a parameter. Further debugging could be done with...
Code:
vmEngland="/home/administrator/vmware/Virtual Machines/VMs/England"
rmdir -p "$vmEngland"
mkdir "$vmEngland"
vmxorig="/home/administrator/vmware/Virtual Machines/VMs/XPVM/Windows XP Pro.vmx"
vmxclone="$vmEngland/pc1/pc1.vmx"
if [ ! -e "$vmxorig" ]; then
  echo "Error: can't find \"$vmxorig\" "
  exit 1
fi
# Should we test that the clone exists before overwriting? 
# Probably not because the directory was removed and recreated.
if [ -e "$vmxclone" ]; then
  echo "Warning: clone exists \"$vmxclone\" "
  read -p "Continue to overwrite? [n] or y> " ans
  if [ "${ans:0:1}" != "y" ]; then
    exit 2
  fi
  rm "$vmxclone"
fi
# This should FAIL unless the clone command can create 'pc1' path for 'pc1/pc1.vmx'.
# Lets make the pc1 directory..
mkdir -p "$vmEngland/pc1/"
/usr/bin/vmrun -T ws clone "$vmxorig" "$vmxclone" linked
if [ ! -e "$vmxclone" ]; then
  echo "Error: can't find \"$vmxclone\" "
  exit 3
fi
rpl "Clone of Windows XP Pro" "pc1" "$vmxclone"
if [ ! -e "$vmxclone" ]; then
  echo "Error: can't find \"$vmxclone\" "
  exit 4
fi
# You'll probably need to create pc2/ and pc3/ directories also.
vmxclone="$vmEngland/pc2/pc2.vmx"
mkdir -p "$vmEngland/pc2/"
# Repeat above script. This could be a lot better as a loop.
# Check a single section to see if I found the problem and 
# then let me know if you want a loop.

You would also need to check what happened to the leading '/' on the path for pc1.vmx. Did the 'rpl' script remove it?

Last edited by m1xram; 12-14-2009 at 06:50 PM.. Reason: eureka! found it
# 6  
Old 12-14-2009
MySQL

Thanks for that. I need some time to obsorb and test it but to answer your question in the meantime:
The 7z command and switches are formatted correctly. It is just like that with 7z.
I am trying to read this stuff: UNIX / Linux Bourne / Bash Shell Scripting Tutorial [ steve-parker.org ] and I see there is plenty to read here too.
Will respond later.
Thanks for the REM comment/reply. I know this much now from steve-parker and you. I appreciate the feedback and lessons.
# 7  
Old 12-14-2009
Is gedit safe to put these scripts together? No nasty hidden characters or formatting...?
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Ubuntu

Copy existing Ubuntu to boot from USB

Hello all, I am looking for a way to copy the existing Ubuntu server 12.04 to a USB (with all the packages and such) and make it boot from the USB. I have seen other threads about copying the CD image to the USB, which is not exactly I am looking for. Before I start diving into anything I... (4 Replies)
Discussion started by: br1an
4 Replies

2. Shell Programming and Scripting

Building a DOS .bat file that will root my Droid 2 phone using adb

1. What I am doing? Building a DOS .bat file that will root my Droid 2 phone using adb commands in the .bat file. (Just for the fun of it and to help me learn "stuff".) 2. Problem: Here are the problem steps to accomplish this task manually in an adb shell: adb shell (Prompt is $) cd... (2 Replies)
Discussion started by: chrstdvd
2 Replies

3. IP Networking

Need to copy file from Linux to DOS.

I have two PCs with Ubuntu 10.4 and DOS 5.0, which are connected with a 9 pins serial cable. I need to copy some files from the Linux box to the DOS box. I tried UUCP but it's too difficult and i didn't found a working client for DOS. Can you help me? Thanks for any reply! (10 Replies)
Discussion started by: mghis
10 Replies

4. Windows & DOS: Issues & Discussions

How to connect SFTP(Linux) from Windows DOS

I need to write a batch script for file transfer from SFTP to Windows system. SFTP is on Linux system. I kept this code in batch file and executing it.. but not working.. Even i tried from Command prompt like this "open sftp.host.com" but getting error. Can anyone help with the code and tell me how... (15 Replies)
Discussion started by: mohantmk
15 Replies

5. Windows & DOS: Issues & Discussions

Awk script in DOS and Linux behaves differently :(

Hi, I have an awk script which performs simple operations of variable assignments and finally printing the variables in custom form. BEGIN {FS=OFS="\n"} { v1=substr($0,1,15) v2=substr($0,16,200) v3=substr($0,216,20) print v1 "|" v2 "|" v3 } The input file being processed... (2 Replies)
Discussion started by: vidyak
2 Replies

6. Shell Programming and Scripting

Building a Linux like LS for Unix

I'm trying to customize my environment at work in a Unix system. So I'm starting with the ls command... I'd like to make it run as the Linux ls... Even in color, if possible... My first problem is to make the file listing brake in columns... In fact, the actual ls from Unix does this, but... (3 Replies)
Discussion started by: 435 Gavea
3 Replies

7. Windows & DOS: Issues & Discussions

rediretion and pipes in DOS shell cmd interpreter

Hello, I am trying to accomplish the following. Send the output of a command to the screen (this happens by default) as well as capture the output of the screen to a log file. How can this be achieved in DOS command interpreter syntax. Any ideas/suggesstions/indicators are greatly... (2 Replies)
Discussion started by: jerardfjay
2 Replies

8. UNIX for Dummies Questions & Answers

DOS Debug for linux

Hi guys. I'm in desperate need of a program that can allow me to write files to different sectors of a disk (floppy), in the same way the DOS Debug program can (with a command such as -w 100 0 0 1 to write the bootsector of the disk). Does anyone know any links or names of programs that could... (2 Replies)
Discussion started by: KrazyGuyPaul
2 Replies
Login or Register to Ask a Question