Linux - NewbieThis 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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
But of course, if I am re writing to a card that already has an image that became corrupt, after
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?
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?
I finally went for a dirty fix on this particular occasion with this little line:
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.
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.
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:
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
iface lo inet loopback
# The primary network interface
iface eth0 inet static
# dns-* options are implemented by the resolvconf package, if installed
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
cat > $outfile << EOF
my text with $variable to be expanded
variables are expanded as usually.
And last not least, if you only want to output some text to stdout, simply write
cat << 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...