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!
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
@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.
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.
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.
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
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.