LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 06-20-2009, 11:41 AM   #1
houms
LQ Newbie
 
Registered: Jan 2009
Posts: 8

Rep: Reputation: 0
bash script using sed or awk?


I am trying to create a batch script that replaces a portion of a file.
but the replacement "text"(number really) is input by the user
for example file name is
/etc/file.conf

and the line that is there is something like [1-*] (where * is any number from 1-6).
the script prompts the user to enter a number from 1-6, and based on that input, the script should replace the * in the existing /etc/file.conf with what the user entered.

so far i have got:
echo "Enter the number (1-6)?"
read NUMBER
cat ~/test | sed -r "s/1-./1-5/" > ~/test

The only thing I can't figure out is how to replace 5 above with the NUMBER variable from read? Is this acceptable way? are their any suggestions whether or not I should be using awk, or grep for that Thanks in advance for you help...

I also have a question within this same script on how to see if a file exists and if it does, then comment out a specific line within that file?
 
Old 06-20-2009, 11:56 AM   #2
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: Slackware®
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Hi,

It does look like homework to me. I've seen this type of problem before. You should approach your instructor with this type of query.

If you need additional reference then look at 'Advanced Bash-Scripting Guide'.

This link and others are available from 'Slackware-Links'. More than just Slackware® links!
 
Old 06-20-2009, 11:59 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
To use a variable inside a SED command, simply insert the value of the variable---in your example "$NUMBER". Note that this requires double-quotes on the command (as you have shown).

Quote:
how to see if a file exists and if it does, then comment out a specific line within that file?
Use the "test" command (AKA "[")
e.g.:

Code:
if [ filename ]; then
     <do stuff>
fi
look at "man test" for the details of the syntax
 
Old 06-20-2009, 12:03 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
The following should work:
edit: removed for the reasons stated before.

sorry guys, did not see your comments

Last edited by sycamorex; 06-20-2009 at 12:05 PM.
 
Old 06-20-2009, 12:33 PM   #5
houms
LQ Newbie
 
Registered: Jan 2009
Posts: 8

Original Poster
Rep: Reputation: 0
@onebuck
its not at all actually. lol. I wish i was in school for this stuff though. So there is no instructor except for myself and you guys, the great community.

@pixellany
thanks for the suggestions, will give it a go and let you know.

the main reason I am trying to do this is i want to write a script for Debian based and ubuntu based distros to disable extra tty's at the users discretion.

for debian based distros its just a matter of commenting out the
entries for each tty in inittab,

but for ubuntu based, its changing the value in /etc/default/console-setup
and then commenting out each ttyx file that you disable. little more complicated. But everywhere I read its best to learn by doing and scratching an itch. Well my personal itch is having to disable extra tty's after install because both systems, and those derived from them, have 6 consoles enabled by default.

Eventually it would be nice to maybe try and create a gui for it.
 
Old 06-20-2009, 12:55 PM   #6
houms
LQ Newbie
 
Registered: Jan 2009
Posts: 8

Original Poster
Rep: Reputation: 0
so using the variable as described works . so thank you so far i got

echo "Enter the number (1-6)?"
read NUMBER
cat ~/test | sed -r "s/1-./1-$NUMBER/" > ~/test

so now i have to edit each /etc/events.d/ttyx
where x is &NUMBER+1+n =<6 (sorry again newbie thinking aloud)
so if the use inputs 3 then tty4 tty5 and tty6 need to be edited.

and basically comment out the
"start on runlevel" lines.

Though can i just rename them to ttyx.bak? would that have the same effect?
I am trying to wrap my head around this as based on that $NUMBER i need to be able to reuse it. by that i mean if i run the script again the line will have to be uncommented, or the file name must be changed back to ttyx from ttyx.bak.
 
Old 06-20-2009, 02:35 PM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by houms View Post
so using the variable as described works . so thank you so far i got

echo "Enter the number (1-6)?"
read NUMBER
cat ~/test | sed -r "s/1-./1-$NUMBER/" > ~/test

so now i have to edit each /etc/events.d/ttyx
where x is &NUMBER+1+n =<6 (sorry again newbie thinking aloud)
so if the use inputs 3 then tty4 tty5 and tty6 need to be edited.

and basically comment out the
"start on runlevel" lines.

Though can i just rename them to ttyx.bak? would that have the same effect?
I am trying to wrap my head around this as based on that $NUMBER i need to be able to reuse it. by that i mean if i run the script again the line will have to be uncommented, or the file name must be changed back to ttyx from ttyx.bak.
Well for the first part it is more concise to do:

Code:
echo "Enter the number (1-6)?"
read NUMBER
sed -i "s/1-./1-$NUMBER/" ~/test
As for the other part, I don't quite get it, can you explain more, and try to write complete sentences.
 
Old 06-20-2009, 03:01 PM   #8
houms
LQ Newbie
 
Registered: Jan 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thank you for your reply.I will give that edit a try.

Here is how i accomplish what i want to do now. this is the manual process i want to automate....

Step1
The first thing that needs to be done is change the setting that says how many tty's are available

To do this, you have to edit /etc/default/console-setup

sudo nano /etc/default/console-setup

Change the ACTIVE_CONSOLES=”/dev/tty[1-6]” setting to reflect how many tty's you want available. If for example you want 4 consoles active, then the line would then be:

ACTIVE_CONSOLES=”/dev/tty[1-4]”

Step2
now you have to edit the tty files to stop them running on systemboot.

cd /etc/event.d/

Now you will change the ttyx files that you don't want. Edit them and comment lines starting with “start on runlevel”. There will be two lines in each file to comment out. In my example, you'll comment the start lines in tty5 and tty6 files.

so the first part i posted is to accomplish the first step, and now i am trying to accomplish the second part using the variable from the first step. at least i think so.... thanks for any help you can give me
 
Old 06-21-2009, 03:20 AM   #9
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Alright, I get it now, so for step 2:

Code:
for i in $(seq $(expr $NUMBER + 1) 6)
do
  sed -i 's|^start on runlevel|#start on runlevel|g' tty$i
done
 
Old 06-21-2009, 04:17 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by houms View Post
I am trying to create a batch script that replaces a portion of a file.
but the replacement "text"(number really) is input by the user
for example file name is
/etc/file.conf

and the line that is there is something like [1-*] (where * is any number from 1-6).
the script prompts the user to enter a number from 1-6, and based on that input, the script should replace the * in the existing /etc/file.conf with what the user entered.

so far i have got:
echo "Enter the number (1-6)?"
read NUMBER
cat ~/test | sed -r "s/1-./1-5/" > ~/test

The only thing I can't figure out is how to replace 5 above with the NUMBER variable from read? Is this acceptable way? are their any suggestions whether or not I should be using awk, or grep for that Thanks in advance for you help...

I also have a question within this same script on how to see if a file exists and if it does, then comment out a specific line within that file?
sed or awk? definitely awk. you can do what you are doing with just one awk script.
Code:
awk 'BEGIN{
  printf "Enter a number: "
  getline num < "-"
  if (!( num > 0 && num <= 6 )){
     print "number not ok"
     exit
  }
}
/\[1-.\]/{
 gsub(/\[1-.\]/,"[1-"num"]")
}1
' /etc/conf.file
 
Old 06-21-2009, 09:14 AM   #11
houms
LQ Newbie
 
Registered: Jan 2009
Posts: 8

Original Poster
Rep: Reputation: 0
@Tex_mex

Thank you for that, it works great. I only have one issue though. If I run it again, it does not have the ability to uncomment the "startup on runlevel".

For example if a user wants to re-run the script and reenable more consoles, the first part of the script will change the value in /etc/default/console-setup

but the second part you provided cannot uncomment any lines in the tty files?

can i add something like this??will that work? I know I know, try and see :P....

Code:
for i in $(seq $(expr $NUMBER - 1) 1)
do
  sed -i 's|#start on runlevel|start on runlevel|g' tty$i
done


@ghostdog,
Thank you for that script, but I wanted to ask, that seems to only ask for the number and replace the number in /etc/default/console-setup, but doesn't do what I need for the ttyx files..


Thank you both for your help. I really appreciate you taking the time, I am learning a lot in the process.


@tex_mex, my suggestion that does not work at all, but i assume you knew that lol, but I am basically trying to uncomment the ttyx files, if they are commented out (from previously running the script) for example if you run script and choose 3, then 1 2 and 3 should be uncommented, and 4 5 and 6 should be commented. but supposed you later realize you want/need 5 ttys, so re-running the script choosing 5, 1 2 3 4 and 5 now need to be uncommented and 6 remains commented. not sure how to go about that......Thanks again

Last edited by houms; 06-21-2009 at 09:56 AM. Reason: update
 
  


Reply



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
Help with BASH script and AWK and SED NickJH Linux - Newbie 11 03-07-2009 04:08 PM
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 09:30 AM
N00b's bash, sed, or awk question petcherd Slackware 5 08-01-2007 06:42 AM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:15 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