LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-14-2016, 01:19 PM   #1
iFunction
Member
 
Registered: Nov 2015
Posts: 185

Rep: Reputation: Disabled
How to get past prompt in bash script


Hi there,

I am trying to write a short script that will write an sd card image for raspberry pi. The main part of the script is here:
Code:
(echo d; echo ; echo d; echo o; echo n; echo p; echo 1; echo +64M; echo t; echo b; echo n; echo p; echo 2; echo ; echo ; echo w | sudo fdisk /dev/sda

sudo mkfs.vfat /dev/sda1
sudo mkfs.ext4 -j /dev/sda2
But of course, if I am re writing to a card that already has an image that became corrupt, after
Code:
sudo mkfs.ext4 -j /dev/sda2
there is a prompt telling that a file system already exists and asks to proceed anyway?

This is not a problem for me, but I would rather it did just do it anyway, as the person using this script would not be formatting the card if it didn't need doing. How can I automatically get past these kind of prompts?

kind regards
iFunc
 
Old 01-14-2016, 01:55 PM   #2
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Rep: Reputation: 2
do
dd if=/dev/zero of=/dev/sda bs=512 count=1

This will erase boot sector. You can increase count.
Or try it on sda1 and 2.

then try
mkfs.ext4 <options>


also check
man mkfs.ext4 for some kind of force option.

Last edited by PoleStar; 01-14-2016 at 02:01 PM.
 
Old 01-14-2016, 06:52 PM   #3
iFunction
Member
 
Registered: Nov 2015
Posts: 185

Original Poster
Rep: Reputation: Disabled
No, that does not work.
 
Old 01-14-2016, 07:42 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,253

Rep: Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559
You can try adding the '-F' option to mkfs, be warned you must use this option twice and it will force building a new file system, even if the filesystem is in use or mounted
 
Old 01-14-2016, 08:16 PM   #5
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 518

Rep: Reputation: 177Reputation: 177
How about:
Quote:
-q Quiet execution. Useful if mke2fs is run in a script.
 
Old 01-14-2016, 08:59 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,937

Rep: Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520Reputation: 1520
Just deleting the partition table will not work since the filesystems remain untouched. The OP's script recreates the partitions that were deleted with the dd command. The dd command would need to zeroize the filesystem metadata of sda2.

I have not check the mkfs version on the Pi and I might be behind the times but none of my systems prompt me. Have you verified that the filesystems are not mounted before running the script?
 
Old 01-15-2016, 01:51 AM   #7
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,176

Rep: Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837Reputation: 837
Quote:
Originally Posted by PoleStar View Post
do
dd if=/dev/zero of=/dev/sda bs=512 count=1
sometimes, when nuking a hard drive, this command was not sufficient, but after i just increased the amount of zeros written, i could proceed:

dd if=/dev/zero of=/dev/sda bs=512 count=1000
 
Old 01-16-2016, 04:56 AM   #8
iFunction
Member
 
Registered: Nov 2015
Posts: 185

Original Poster
Rep: Reputation: Disabled
Hi there,

I finally went for a dirty fix on this particular occasion with this little line:
Code:
echo "y" | sudo mkfs.ext4 -j /dev/sda2
It's not a solution I would normally use to circumnavigate a prompt, but it works and I have had to use this way in the fdisk line also, which actually generates an error message if a brand new sd card is used, it doesn't effect the outcome, just has a nasty red blemish saying that a command was not found and then moves on to the next one.

@michealk, if you are using a brand new disk, no it does not prompt, if however I was testing script, and only want to use the one card, so yes everything can be wiped all the time, this script is so that in the event that a pi becomes corrupt, there is a very simple solution for building a new one out in the field and it is likely that the same sd card will be used, and I can guarentee it will not get wiped first. As a result, mkfs sees the old ext4 file structure on the partition and asks if you are sure you want to overwrite it. This is where the sticking point is.

Thanks for all your advice, I will likely be looking into this in future, as this is bound to come up again, and I would like to find a nicer solution, but for what I need and time constraints, this will do.

Kind regards
iFunk
 
Old 01-16-2016, 03:28 PM   #9
colin.mackellar
LQ Newbie
 
Registered: Oct 2012
Location: Barnack, Stamford, UK
Distribution: Centos, Ubuntu & Oracle
Posts: 4

Rep: Reputation: Disabled
How about "expect" to handle any prompts that come out of the script?
 
Old 01-16-2016, 06:33 PM   #10
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Not a direct reply for your question but you might want to take a look at parted for the task you are currently solving with fdisk.

It's probably a little less painful for use in scripts than fdisk. In fact, it even has a --script flag:
Code:
       -s, --script
              never prompts for user intervention
 
1 members found this post helpful.
Old 01-17-2016, 03:46 AM   #11
iFunction
Member
 
Registered: Nov 2015
Posts: 185

Original Poster
Rep: Reputation: Disabled
Hi Joe,

I will look into this, thank you. I do also use parted, though as I am new to scripting, I am still in the realm of learning how to pass commands to and from scripts, I went for fdisk on this occasion, as the are only 64Gb sd cards and fdisk only uses single character interaction. I am currently trying to work out how you can change the IP address of the newly built sd card, and I am facing the same issue, how do I edit a file in the correct place in a script.

I have still to learn the joys of grep and sed.

Kind regards
iFunk
 
Old 01-17-2016, 04:38 AM   #12
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
When writing more text to files I find the "here document" functionality very useful in scripts.
Consider the following example to write the desired content to /etc/network/interfaces:

Code:
#!/bin/bash
outfile=foobar
cat > $outfile << "EOF"
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet static
       address 192.168.1.103
       netmask 255.255.255.0
       network 192.168.1.0
       broadcast 192.168.1.255
       gateway 192.168.1.1
       # dns-* options are implemented by the resolvconf package, if installed
       dns-nameservers 192.168.1.1
EOF
Note that the content to be written can even include $ symbols etc. The quotes around the EOF prevent variables from being expanded. If you just write
Code:
cat > $outfile << EOF
my text with $variable to be expanded
EOF
variables are expanded as usually.

And last not least, if you only want to output some text to stdout, simply write
Code:
cat << EOF
mytext
EOF
This is useful to e.g. print help texts etc...

Long story short: If your aim is to write network configuration, I would not grep / sed anything, I would write a completely new file to make sure I get exactly what I want.
And if for some reason you must keep the existing file and only modify it, consider the use of diff & patch for such purposes...
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace login prompt with interactive bash script on serial port neo3matrix Linux - General 1 01-21-2015 01:07 PM
Looking for a bash script which will prompt to enter host & username at initial boot. abc_19 Ubuntu 2 02-17-2011 11:52 AM
openSUSE 11.1 Fails to boot past Grub prompt HellesAngel SUSE / openSUSE 6 01-30-2010 09:20 AM
Cannot get past install at kboot prompt - YDL 6.0 on PS3 valentino46 Linux - Newbie 7 08-22-2008 07:25 PM
How to get past websites authentication in a bash script? bruno buys Linux - Software 6 01-12-2006 10:46 AM


All times are GMT -5. The time now is 06:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration