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
# 8  
Old 12-14-2009
Quote:
Originally Posted by dp123
Is gedit safe to put these scripts together? No nasty hidden characters or formatting...?
Yes.
# 9  
Old 12-16-2009
OK. Thanks m1xram, from what you gave me I have done some work to come up with the script below.
PC3 isn't a clone job but an unzip job followed by a 'create it's vmdisk' job (using vmware-vdiskmanager)
1. This script isn't 100% yet and I appreciate your experienced eyes and advice here.
It works until the second last instruction and that is 7z, which doesn't quite work. It unzips the files to a new subdierctory in the target directory (names %0D). It works when run manually line by line... Please see my hashed out notes at the bottom.
I left out the error checking and messages, mostly because I don't understand them yet (I can interperte though) and also because the files will always be there. I will make sure of that.
2. Once the 7z is fixed (I hope it can be) I want to make another two countries, Ecuador and Chile (with PCs 4, 5 and 6 and PCs 7, 8 and 9 in them) and I planned on having similar scripts for them but can it be done in one, with the "read" command for example or some other fancy way?
Thanks,
D


Code:
#!/bin/bash
country=England
echo $country
vmEngland="/home/administrator/vmware/Virtual Machines/VMs/England"
echo $vmEngland
rm -rf "$vmEngland"
echo "==========================================================="
echo "Clean up done"
echo "==========================================================="
read -p "Press Enter to continue . . ."
mkdir "$vmEngland"
echo "==========================================================="
echo "Country directory created."
echo "==========================================================="
read -p "Press Enter to continue . . ."
vmxorig="/home/administrator/vmware/Virtual Machines/VMs/XPVM/Windows XP Pro.vmx"
# original line -- vmxclone="$vmEngland/pc1/pc1.vmx"
vmxpc1="$vmEngland/pc1/pc1.vmx"
vmxpc2="$vmEngland/pc2/pc2.vmx"
vmxpc3="$vmEngland/pc3/pc3.vmx"
pc37z="/var/VMs/7z/pc3.7z"
pc3dir="/home/administrator/vmware/Virtual Machines/VMs/England/pc3/"
pc3disk="/home/administrator/vmware/Virtual Machines/VMs/England/pc3/pc3-000001.vmdk"
# not working --> for i in $vmxpc1 $vmxpc2
# not working ---> /usr/bin/vmrun -T ws clone "$vmxorig" "$i" linked
echo "==========================================================="
echo "Creating PC1 and PC2 (as clones)...."
echo "==========================================================="
/usr/bin/vmrun -T ws clone "$vmxorig" "$vmxpc1" linked
/usr/bin/vmrun -T ws clone "$vmxorig" "$vmxpc2" linked
echo 
echo "==========================================================="
echo "Renaming clones ...."
echo "==========================================================="
rpl "Clone of Windows XP Pro" "$pc1" "$vmxpc1"
rpl "Clone of Windows XP Pro" "$pc2" "$vmxpc2"
echo 
echo "==========================================================="
echo "PCs Done. Going ahead with PC 3 now ..."
echo "==========================================================="
echo
read -p "Press Enter to continue . . ." 
clear
echo "==========================================================="
echo "Unzipping PC 3 and creating Hard drive ...."
echo "==========================================================="
# THIS NEXT LINE WORKS IN THE COMMAND LINE WHEN PASTED IN BUT NOT PROPERLY WHEN
# IN THIS SCRIPT. IT CREATES A DIRECTORY NAMED "%0D" IN $pc3dr AND UNZIPS TO THERE.
7z e "$pc37z" -o"$pc3dir"

/usr/bin/vmware-vdiskmanager -c -s 10240MB -a lsilogic -t 0 "$pc3disk" # Use -t 2 for preallocated 10GB drive (takes 10GB free disk space).

echo "==========================================================="
echo "PC3 Done (including creating 10GB CSI Virtual Hard drive)."
echo "==========================================================="

# exit



---------- Post updated 16-12-09 at 09:01 AM ---------- Previous update was 15-12-09 at 08:26 PM ----------

I have got a temporary working fix for the problem I had. It is not graceful but makes the script 100% functional for what I want.
I would like to understand what is causing the problem with 7z though, if anyone knows?
I am also keen to learn how to improve this and also how I could make the other 2 countries scripts, if I should be making two more country scripts (Ecuador and Chile as mentioned above) like this of if it can all be done in this one.
The fix:
Code:
# First line kind of works
# Line two and three clean up after the first line
7z e "$pc37z" -o"$pc3dir"
mv "$pc3dir/%0D/*" "$pc3dir"
rm -rf "$pcdir%0D/"

# 10  
Old 12-16-2009
fix for loop

Your "for" loop needs quotes.

Code:
for i in "$vmxpc1" "$vmxpc2"; do
  echo "Processing \"$i\"..."
done

As 7zip is a port to Linux maybe it does its own command line thing, taking the raw line and parsing it. Although this is unlikely you could try an "eval" command on it to force the issue. Some escaped quotes will also be required for the test. Let me know the result.

Code:
# TEST1: Do variable substitution before execution.
7z_cmd="7z e \"$pc37z\" -o\"$pc3dir\""
eval $7z_cmd

Another thing to try is quoting the parameter with the switch. This is a very unusual thing to do but might work with their parser.

Code:
# TEST2: Do variable substitution before execution.
7z_cmd="7z e \"$pc37z\" \"-o$pc3dir\""
eval $7z_cmd

I'm surprised that the "vmrun" command worked without creating directories for "$vmEngland/pcX". Does "vmrun" create the "pcX" part of the path?

One other note. /var hangs around forever. /tmp gets cleaned up periodically.
# 11  
Old 12-17-2009
Thanks m1xram,

The vmrun command does indeed create the target directories, successfully. This is very handy.
I gave up on 7z and just used the unzip command (see second code window below). Of course I first had to unzip the .7z files and re-archive them as .zip for unzip to work.
I also had to create the directories to unzip to. 7zip does that automatically (but not succefully in this case!)

I used /var so that the files would be 'safe' and tucked away for whenever they are needed by the script.

I have searched but cannot find the answer to another question. Maybe you can help me. Can you launch a program (vmware console in this case) at the end of the script and allow the program to carry on running in the background, while the script exits gracefully? In the first code window below is the command to run last thing in the script.
I am also copying it to the desktop in the form of a seperate script file (to double click and click Run), which works.
I thought nohup would help with an & but vmware console doesn't stay open after the script exits...?

Code:
vmware "$vmxpc1"  "$vmxpc2"  "$vmxpc3"

Code:
pc3zip="/var/VMs/zip/pc3.zip"
pc3dir="/home/administrator/vmware/Virtual Machines/VMs/England/pc3/"
pc3disk="/home/administrator/vmware/Virtual Machines/VMs/England/pc3

mkdir "$pc3dir"
unzip "$pc3zipzip" -d "$pc3dir"

# 12  
Old 12-17-2009
fork from parent

From what I understand you can do something like...

Code:
( ( cmd & ) & )

A Linux Guru could explain better why this works but I believe the run-in-background fork that '&' creates detaches from the parent. So I think the first '&' detaches from the process that created it and then next one detaches from the login process. I've tried it without the second set of ()s and it doesn't work so all ()s are required. Some commands know how to do this on their own like dameons.

Usually for scripts where you intend to stay logged in, one '&' is sufficient.

Another way to do this, and I think your goal is to eventually run this from cron periodically, is to spawn off the parallel jobs and wait for them to complete. The simpliest way is...

Code:
backup node1 &
backup node2 &
wait
echo "TIMESTAMP: Backup jobs complete."

'wait' waits for all the subprocesses to complete. This might create some confusion in your log files. I tried an experiment with detached processes...

Code:
#!/bin/bash

for x in $(seq 1 $1); do

    sleep $2
    echo "Proc $$, loop $x"
done

And ran them with...
Code:
./backuptest.sh 5 2 &
./backuptest.sh 10 1 &

...to see what the output looks like and it is a bit yucky. Add a easy to read prefix to your output to differentiate the jobs like "London" or "Manchester" and a timestamp instead of "Proc $$", the process id.

I remember doing a backup job some time ago. The one thing that bugged me was finding files. You create this gargantuan zip or tar gzip file and then want to restore a single file. Searching for that file in the archive is very time consuming unless you also create a directory listing with the archive. The directory listing is much smaller and faster to search. The last part of the zip job could be...

Code:
unzip -l $ARCHIVE > ${ARCHIVE/\.zip/.lst}

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