LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-19-2010, 08:01 AM   #1
chutsu
Member
 
Registered: Nov 2003
Location: UK
Distribution: Debian Lenny
Posts: 255

Rep: Reputation: 31
can one create a bash script to remove all partitions on harddrive via fdisk?


I would like to remove/create partitions on a harddrive without going into the menu of fdisk.

can one pass options to fdisk to create and delete without going into the menu? So that I can write a script to do that automatically.

Or if fdisk isn't capable, what other alternatives do I have?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-19-2010, 08:13 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380
Hi,

Yes you can if you use a here document.

An example:
Code:
#!/bin/bash

fdisk /dev/sda >/dev/null 2>&1 <<EOF
n
p
2
+500M
w
EOF

exit
The here document part, all betweem "EOF and EOF (at the beginning on the line) is given to fdisk and executed by fdisk.

In the above example you create a new partition (n) on /dev/sda, that is primary (p) and partition number 2 (2) with a size of 500Mb (+500M). The result is saved (w).

As you might have noticed the letters/numbers used in the here document are exactly the same as those used when using the fdsik menu.

You can use variables inside the here document if you wish to do so.

Do try this with none-permanent statements first.

Hope this helps.
 
2 members found this post helpful.
Old 07-19-2010, 08:14 AM   #3
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad W520
Posts: 5,178

Rep: Reputation: 1426Reputation: 1426Reputation: 1426Reputation: 1426Reputation: 1426Reputation: 1426Reputation: 1426Reputation: 1426Reputation: 1426Reputation: 1426
Use parted instead, see "man parted" or "info parted".
 
0 members found this post helpful.
Old 10-06-2010, 12:16 AM   #4
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
Does that script works under an IF statement? I tried doing that but the letters turned into color red in the syntax highlighting of vim.

Regards,
archieval
 
Old 10-06-2010, 12:17 AM   #5
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
Does that script works under an IF statement? I tried doing that but the letters turned into color red in the syntax highlighting of vim.

Regards,
archieval
 
Old 10-06-2010, 12:25 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380
Hi,

It works when using an if-then-else -fi statement. I am assuming shell scripting(bash/ksh), haven't tried this with other programming languages that use this.

Hope this helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
weird fdisk problem (Fdisk unable to see winxp partitions) khidot Slackware - Installation 5 04-07-2007 09:26 PM
FDISK does not let me create partitions millikin691@hotmail. Slackware - Installation 54 08-28-2006 07:04 PM
fdisk -l output with partition label appended - Bash script available Emmanuel_uk Programming 7 06-10-2006 05:08 AM
/bin/bash how to remove specific file anywhere on harddrive cjagdish69 Linux - Software 1 01-07-2005 05:42 AM
Fdisk Bash SCript zael Programming 1 09-21-2003 04:54 AM


All times are GMT -5. The time now is 01:17 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration