LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-26-2008, 04:24 PM   #1
alirezan1
Member
 
Registered: Nov 2004
Location: Vancouver
Distribution: Ubunty, CentOS ,Mandriva, Gentoo, RedHat, Fedora, Knoppix
Posts: 150

Rep: Reputation: 15
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
 
Old 08-26-2008, 04:56 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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 ;-)
 
Old 08-26-2008, 05:07 PM   #3
alirezan1
Member
 
Registered: Nov 2004
Location: Vancouver
Distribution: Ubunty, CentOS ,Mandriva, Gentoo, RedHat, Fedora, Knoppix
Posts: 150

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna View Post
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?
 
Old 08-26-2008, 05:42 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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!
 
Old 08-26-2008, 05:55 PM   #5
alirezan1
Member
 
Registered: Nov 2004
Location: Vancouver
Distribution: Ubunty, CentOS ,Mandriva, Gentoo, RedHat, Fedora, Knoppix
Posts: 150

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna View Post
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!
 
Old 08-26-2008, 06:21 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 08-27-2008, 02:21 AM   #7
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by druuna View Post
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.
 
Old 08-27-2008, 08:15 AM   #8
rob.rice
Senior Member
 
Registered: Apr 2004
Distribution: slack what ever
Posts: 1,076

Rep: Reputation: 205Reputation: 205Reputation: 205
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a string in bash Agentrooker Programming 5 08-11-2008 03:04 PM
executing Bash Scripts c4nk Linux - General 3 03-11-2007 04:52 PM
a bash file executing only "ls -l" doesn't produce colorful listing sirbijan Linux - General 2 08-06-2006 06:19 AM
Bash problem, no executing of string ChoKamir Programming 3 01-31-2005 04:50 AM
Bash, input validation: request for comments unSpawn Programming 3 07-25-2003 08:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration