LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   executing comments from a file in BASH? (http://www.linuxquestions.org/questions/linux-general-1/executing-comments-from-a-file-in-bash-665604/)

alirezan1 08-26-2008 04:24 PM

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:

,,E
,,
,,
,,
,999,L
,930,L
,190,L
,1200,L
,5000,L
,3876,L
,1200,L
,,L
and so, what I want is to execute:

Quote:

sfdisk /dev/hda << EOF
,,E
,,
,,
,,
,999,L
,930,L
,190,L
,1200,L
,5000,L
,3876,L
,1200,L
,,L
EOF
can someone help me figure out how to do this please?

Thanks

druuna 08-26-2008 04:56 PM

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 ;-)

alirezan1 08-26-2008 05:07 PM

Quote:

Originally Posted by druuna (Post 3260716)
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 ;-)


Alright, that scared me! You have any other suggestions?

druuna 08-26-2008 05:42 PM

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!

alirezan1 08-26-2008 05:55 PM

Quote:

Originally Posted by druuna (Post 3260747)
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!

Great! Thanks for the info! Very useful. I actually, am testing my script on a virtual machine running Knoppix 5.1.1 . I figured, using virtual machine and virtual disk, I can do anything without fearing of these side effects of buggy scripts...

Thanks for the comments!

jschiwal 08-26-2008 06:21 PM

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.

Valery Reznic 08-27-2008 02:21 AM

Quote:

Originally Posted by druuna (Post 3260747)
Hi,


- 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.

sfdisk not targeting complete disk it's change only MBR (or more precisely partition table in the MBR) so dd if=/dev/sda of=backup bs=512 count=1 will be enough.

rob.rice 08-27-2008 08:15 AM

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:13 PM.