Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
- 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!
- 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...
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.
- 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.