executing comments from a file in BASH?
Hi guys,
I have a file that has few info in it that I need to execute in bash... Here's the details: I am using sfdisk to automate partitioning of hard disk. To do this, I extract the information I need (start and sizes of each existing partition) from sfdisk -l and write them in a .txt file in the form of: start,size,L which is easy input for sfdisk. I can't use sfdisk -d output file for many reasons which are irrelevant for this topic. What I want to do is, to get the inputs to sfdisk from this .txt file and execute it in bash after I call sfdisk /dev/hda << EOF and before EOF Here's the sample contents of my .txt file: Quote:
Quote:
Thanks |
Hi,
Try this: sfdisk /dev/hda < infile.txt Using a hear-document structure when your input is in a file is not the way to go. You can probably get it to work, but it would be the hard way. I hope you have backups, experimenting with sfdisk can have 'funny' results and typos are made by the best of us ;-) |
Quote:
Alright, that scared me! You have any other suggestions? |
Hi,
Not really, except the 'obvious' stuff: - Be familiar with the commands you are using (especially if you start automating them). - Have backups. In this case you're targeting a complete disk, everything on it will be gone. Check out the sfdisk manpage and look for the -O and -I options. Nonetheless, making a typo (hdb instead of hda) could destroy more important stuff. - If at all possible test the script(s) in a safe environment (or as safe as possible). Most of the time you encounter one or more errors the first time you run a (larger) script. These errors can potentionally be harmful (from a deleted file to wiping a harddisk). In this case you need to be 'extra' careful, sfdisk is a command that needs to be run as root. If you have a 'spare' device around that can be partitioned (usb stick/disk?), use that as your test harddisk while you write/test/debug the script and make very sure sfdisk is given the correct device! |
Quote:
Thanks for the comments! |
A HERE document is used for documents embedded in your script. Suppose that you want to write an xml file from a template. You would enter the document into the bash script after the line with the <<EOF. The lines can include variables which are expanded. Before the package systems we use today, software was distributed in a shell script that contained a here document for each file to produce.
If I were to use fdisk in a script, I would use the -u option so that the begin and end values are based on 512 byte blocks. This eliminates rounding errors when you read the disk. For example, if I had an image file of a disk, I would use the fdisk start value to determine the offset for losetup to be able to mount the file system. I'm not at my Linux machine right now, so I can't bring up the cfdisk manpage to see if there is a similar problem. One thing you might want to look at is the set command to debug your script. After assigning values to variables from a line of data, instead of calling cfdisk, use something like "set $OPTION $START $END $DEVICE" instead and examine the values of $1, $2, $3 & $4 to make sure they are what you expected. The set command will setup the argc and argv array that the cfdisk command would see. |
Quote:
|
you could add
#!sfdisk /dev/hda as the first line then chmod +x the file and run it as a script but for some thing like this I would do it by hand |
All times are GMT -5. The time now is 06:19 AM. |