Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] "piping" options to a command, such as fdisk
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
bob doe
Apprentice
Apprentice


Joined: 11 Jun 2007
Posts: 207

PostPosted: Fri Jun 27, 2008 2:01 pm    Post subject: [SOLVED] "piping" options to a command, such as fd Reply with quote

Hello all, Im hoping there are a few Bash gurus out there willing to lend a hand :)

Im in the process of creating an installer script for my LiveCD which would install to a hard drive (duh?). The whole script is a CLI driven step by step process which queries the user for various information.

For instance, in one mode, it will ask the user how large he wishes to make his /boot, SWAP, and / (root) partitions. Say the user wants a 32mb /boot partition, he'd type in '32M'. The problem is, how can I in turn create a fdisk line to do that?

I've only managed to pipe in a single option into a command, but not multiple options. For instance:

echo p | fdisk

will run fdisk, and print the partition map. But if I want to create a 32mb partition, then the process is a little more:
fdisk -> n -> p -> 1 -> (enter) -> 32M

Any ideas how (and if) this can be done?

Thanks!


Last edited by bob doe on Wed Jul 02, 2008 2:30 pm; edited 1 time in total
Back to top
View user's profile Send private message
fank
l33t
l33t


Joined: 16 Oct 2004
Posts: 794
Location: Minsk, Belarus

PostPosted: Fri Jun 27, 2008 2:35 pm    Post subject: Reply with quote

Im not a bash guru =)
please try this:


Code:
fdisk /dev/DEV<<EOF
n
p
1
32M
EOF

_________________
Слово „христианство“ основано на недоразумении; в сущности, был один христианин, и тот умер на кресте.
Back to top
View user's profile Send private message
bob doe
Apprentice
Apprentice


Joined: 11 Jun 2007
Posts: 207

PostPosted: Fri Jun 27, 2008 2:47 pm    Post subject: Reply with quote

I tried that one and it works. However, I couldnt get it to work with a variable. For instance:

echo "how big to make /boot"
read big
(say the user typed 32M)

fdisk /dev/sda<<EOF
n
p

1
$big
w
q
EOF

[edit]
It also seems I cant put the EOF stuff in a script?
Back to top
View user's profile Send private message
kreedcarmoch
n00b
n00b


Joined: 09 Apr 2005
Posts: 51
Location: San Antonio, Texas

PostPosted: Fri Jun 27, 2008 3:26 pm    Post subject: Reply with quote

Code:
echo -e "n\n p\n 1\n 32M" | fdisk
Back to top
View user's profile Send private message
bob doe
Apprentice
Apprentice


Joined: 11 Jun 2007
Posts: 207

PostPosted: Fri Jun 27, 2008 4:32 pm    Post subject: Reply with quote

Thanks that one works. Curious any idea how to use a variable in there? I tried something as such:

echo -e "n\n p\n 1\n "$big | fdisk /dev/sda

But it didnt work.
Back to top
View user's profile Send private message
frostschutz
Advocate
Advocate


Joined: 22 Feb 2005
Posts: 2968
Location: Germany

PostPosted: Fri Jun 27, 2008 6:07 pm    Post subject: Reply with quote

Does it absolutely have to be fdisk? sfdisk was created with piping in mind, you can pipe its output to a file to make a backup of a partition table and pipe the file to sfdisk to restore the backup. It's much easier to handle through pipes than plain old fdisk which is meant to be an interactive app.
Back to top
View user's profile Send private message
kreedcarmoch
n00b
n00b


Joined: 09 Apr 2005
Posts: 51
Location: San Antonio, Texas

PostPosted: Fri Jun 27, 2008 7:06 pm    Post subject: Reply with quote

Code:
echo -e "n\n p\n 1\n $big\n" | fdisk /dev/sda

Just make sure you have a newline after each command. ^_^
Back to top
View user's profile Send private message
kev009
n00b
n00b


Joined: 17 Oct 2005
Posts: 43
Location: Tempe, Arizona

PostPosted: Tue Jul 01, 2008 2:04 am    Post subject: Reply with quote

man xargs
_________________
http://www.kev009.com
Back to top
View user's profile Send private message
bob doe
Apprentice
Apprentice


Joined: 11 Jun 2007
Posts: 207

PostPosted: Wed Jul 02, 2008 2:30 pm    Post subject: Reply with quote

Solved with this line:

echo -e "n\np\n1\n\n+"$p2_boot_size"\na\n1\nn\np\n2\n\n+"$p2_swap_size"\nt\n2\n82\nn\np\n3\n\n+"$p2_root_size"\nw\nq\n" | fdisk $p2_hdd > /dev/null 2>&1
Back to top
View user's profile Send private message
frostschutz
Advocate
Advocate


Joined: 22 Feb 2005
Posts: 2968
Location: Germany

PostPosted: Wed Jul 02, 2008 2:57 pm    Post subject: Reply with quote

If that works for you, okay, it's not a very nice solution though and not easy to apply changes.

To quote the fdisk manpage:
man fdisk wrote:
BUGS
There are several *fdisk programs around. Each has its problems and strengths. Try them in the order cfdisk, fdisk, sfdisk. (Indeed, cfdisk is a beautiful program that has strict requirements on the partition tables it accepts, and produces high quality partition tables. Use it if you can. fdisk is a buggy program that does fuzzy things - usually it happens to produce reasonable results. Its single advantage is that it has some support for BSD disk labels and other non-DOS partition tables. Avoid it if you can. sfdisk is for hackers only - the user interface is terrible, but it is more correct than fdisk and more powerful than both fdisk and cfdisk. Moreover, it can be used noninteractively.)
Back to top
View user's profile Send private message
bob doe
Apprentice
Apprentice


Joined: 11 Jun 2007
Posts: 207

PostPosted: Wed Jul 02, 2008 5:38 pm    Post subject: Reply with quote

I looked fairly briefly at sfdisk but even after reading the man page it was too.... complicated? to use without some Googling or something.

Thankfully though, since this is part of an install script for my LiveCD, I dont see the line changing anytime soon.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum