LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-17-2014, 06:32 PM   #1
kb9mfd
LQ Newbie
 
Registered: May 2014
Posts: 3

Rep: Reputation: Disabled
Modify fstab in a bash function using sed


I have a function to modify fstab using sed but I cannot get it to work correctly - ($1 = /dev/md0)

Code:
modify_fstab () {
  sed -i "s/LABEL=\/boot/$1/g" /etc/fstab
}
That gives a error because $1 has / in it, if I use single quotes the varible is not expanded. How do I get around that? Thanks!
 
Old 05-17-2014, 06:49 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
I usually use [ instead of /

.e.g

Code:
modify_fstab () {
  sed -i "s[LABEL=/boot[$1[g" /etc/fstab
}
You probably don't need the g, not that it matters
 
1 members found this post helpful.
Old 05-17-2014, 08:52 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,251

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159
It would pay to have a very good understanding of sed before screwing with fstab with -i. If you mess that up, you can't boot.
 
1 members found this post helpful.
Old 05-17-2014, 09:14 PM   #4
kb9mfd
LQ Newbie
 
Registered: May 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
I usually use [ instead of /

.e.g

Code:
modify_fstab () {
  sed -i "s[LABEL=/boot[$1[g" /etc/fstab
}
You probably don't need the g, not that it matters
Thank you! I did not realize you can change the delimiter. It worked perfect.
 
Old 05-17-2014, 09:19 PM   #5
kb9mfd
LQ Newbie
 
Registered: May 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
It would pay to have a very good understanding of sed before screwing with fstab with -i. If you mess that up, you can't boot.
Yes, good thing I am working on a non-production unit. I have tested and reloaded this machine about 6 times now to test all the scripts, this was the last thing I could not fully resolve. I was creating a script to convert it from a single drive to raid. Thanks!

(it was setting my boot to $1, it took took a couple of those reloads before I remebered to fix that before rebooting)
 
  


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
\vi/etc/fstab works fine . vi /etc/fstab throws bash command not found in Rescue mode jsaravana87 Linux - Server 1 05-13-2013 02:08 PM
[SOLVED] awk/sed to modify /etc/fstab kbscores Linux - Newbie 3 02-19-2013 10:37 AM
modify fstab keimasi Linux - General 4 03-17-2011 11:27 AM
couldnt modify the /etc/fstab, please help hackintosh Linux - General 1 11-25-2007 08:50 PM
How to modify fstab? SLAY3R Linux - Newbie 7 09-18-2004 11:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:53 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
Open Source Consulting | Domain Registration