SlackwareThis Forum is for the discussion of Slackware Linux.
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.
Why I wrote this script. I wrote this script because I have broken my system several times trying new things. I had gotten tired of customizing my system by hand on every new install. Furthermore, I also can use this script to customize other computers at home which have no network access.
Other then basic bash commands, I learned much of the scripting from rute. See my signature below for the link.
I still consider myself a novice after a two year process of converting from windows to Linux Slackware. So if you wish to give input, please provide info so that even noobs can learn from it.
I invite your feedback on how to improve on the first script I had just finished. I tested parts of the script when developing it in a directory I named "test". However, since I have not been able to install vmware on my current system nor have I broken my system in a while, I have not been able to fully test the new script.
Some background info. I have a 1TB usb drive in which I use as a personal repository for my custom files and slack packages. So, this script will not work on any other computer systems other then my home computers. However, I have no problem with others taking ideas from my script. "Freely I receive, freely do I give."
I have a couple questions that I hope some of you can answer.
1. I used the sed command to edit a file for personal use. It seems that sed will not actually edit a file. From what I understand, sed streams the file in question and replaces the desired changes into a different file. I then have to move the new file to replace the original file. This seems a little awkward. There should be a better way to go about editing a file.
2. I know there are a bunch of ways for non root users to turn off the computer. I happened to try one method by adding a group called "shutdown". Adding it to the /etc/sudoers. Changing ownership of /sbin/halt and /sbin/reboot from root:root to root:shutdown. Adding the shutdown group to specific users. Adding the alias "off"
to .bashrc. Having changed ownership of the /sbin files, I have no problem turning off the computer at runlevel 3 as a user. I am wondering if changing ownership might cause a problem shutting down in runlevel 4?
3. One part of the script that I have yet to figure out is to search and add printers from from from the network. Currently, I have a printer connected to my wife's xp. Her computer name is mom and the shared printer is an HP L7580. Now before someone tries to tell me how to connect to this printer, that is not the problem. I already included that into the script. My question is, perhaps the printer is moved to another computer. Or a second printer has been added to the network as a shared printer. Then the current settings will not work. I want to write a script where all of the computer names are searched out first of all. Then printers are searched. Then all available network printers are added to cups. My question is there a command to search out available printers on a network? Ive googled it but have not found a solution.
Code:
#!/bin/sh
#Because I break my system so many time. I decided to write my firt script to quickly customize things once
#Slackware is installed.
#One assumption I made is that all of my backup files are in their current locations on my usb drive.
#most of the files are located in /backup/Dennis/linux/ directory
#I gave simple explinations so I dont forget what each part is for.
#User input function to see if users agree to each step.
function question ()
{
N=1
while test "$N" -le "3"
do
echo "Do you want to $TITLE ? y/n"; read X
case $X in
y|Y|Yes|YES)
OUTCOME="y"
break
;;
n|N|No|NO)
OUTCOME="n"
break
;;
*)
echo "You did not select y or n"
OUTCOME="n"
;;
esac
echo "You have 3 tries you are on number $N"
if test "$N" = "3" ; then
echo "You did not select yes so your answer will be considered NO!"
fi
N=$[N+1]
done
}
#checking who is executing this script
WHO=`whoami`
if test $WHO != "root"; then
echo "You need to be in root to execute this script!"
exit
fi
#Confirming mounting usb drive.
echo "***Mounting backup files ***"
if test -e /backup = "" ; then
mkdir /backup
MYDISK=`fdisk -l | grep 11918 | grep -o '/dev/sd..'`
fi
# unmount the default mount of /media/disk or /media/disk-1
umount "$MYDISK"
mount "$MYDISK" /backup
#test to see if disk exists.
if test "$MYDISK" = "" ; then
echo "***You need the usb WD drive to run this script***"
exit
fi
echo "***Install essential dellfand first***"
#prevents overheating of cpu
cp /backup/Dennis/linux/tar/* /usr/local/src
tar jxf /usr/local/src/dellfand-0.9.tar.bz2
cd /usr/local/src/dellfand-0.9 ; make
./dellfand 1 10 38 39 45
chmod +x dellfand ; cp dellfand /usr/local/bin/
echo "dellfand 1 10 38 39 45"
echo "***Installing Custom Startup Scripts and custom files***"
cp /etc/rc.d/rc.local /etc/rc.d/rc.local.BAK
cp /etc/rc.d/rc.inet1.conf /etc/rc.d/rc.inet1.conf.BAK
cp /etc/rc.d/rc.wireless.conf /etc/rc.d/rc.wireless.conf.BAK
cp /etc/wpa_supplicant.conf /etc/wpa_supplicant.conf.BAK
cp /etc/modprobe.d/blacklist /etc/modprobe.d/blacklist.BAK
cp /etc/samba/smb.conf /etc/samba/smb.conf.BAK
cp /etc/cup/printers.conf /etc/cup/printers.conf.BAK
cp /etc/sudoers /etc/sudoers.BAK
cp /backup/Dennis/linux/administration/custom_file_backups/rc.* /etc/rc.d
cp /backup/Dennis/linux/administration/custom_file_backups/wpa_supplicant.conf /etc/
cp /backup/Dennis/linux/administration/custom_file_backups/blacklist /etc/modprobe.d/
cp /backup/Dennis/linux/administration/custom_file_backups/bash /root/.bashrc
cp /backup/Dennis/linux/administration/custom_file_backups/smb.conf /etc/samba/
cp /backup/Dennis/linux/administration/custom_file_backups/printers.conf /etc/cups/
cp /backup/Dennis/linux/administration/wallpaper/*.jpg /usr/share/wallpapers/
chmod -x /etc/profile.d/bsd-games-login-fortune.sh
#Editing /etc/sudoers for the purpose of adding shutdown group so users
#can shut off the computer.
chmod +w /etc/sudoers
echo "%wheel ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
echo "***Adding shutdown group for users to shut down***"
groupadd shutdown
echo "%shutdown localhost=/sbin/shutdown -h now" >> /etc/sudoers
echo "%shutdown localhost=/sbin/reboot" >> /etc/sudoers
chown root:shutdown /sbin/halt
chown root:shutdown /sbin/poweroff
chown root:shutdown /sbin/reboot
chown root:shutdown /sbin/shutdown
chmod 440 /etc/sudoers
# Use wicd instead of stock /etc/rc.d/inet1 network connections
TITLE="disable slackware and install WICD"
question
if test "$OUTCOME" = "y" ; then
chmod -x /etc/rc.d/rc.inet1
installpkg /backup/Dennis/linux/wireless/wicd*.tgz
echo "ifconfig lo up" >> /etc/rc.d/rc.local
fi
#Install personal packages from usb hdd
TITLE="install personal packages"
question
if test "$OUTCOME" = "y" ; then
installpkg /backup/Dennis/linux/tgz/*.tgz
cp /backup/Dennis/linux/administration/custom_file_backups/kismet.conf /etc/kismet/
fi
#Install games from usb hdd
TITLE="install games"
question
if test "$OUTCOME" = "y" ; then
installpkg /backup/Dennis/linux/games/*.tgz
fi
#***Adding users***
TITLE="add a user"
question
while test "$OUTCOME" = "y"
do
echo "Add a user name";read NM
useradd -g users -G slocate,disk,cdrom,plugdev,sys,lp,shutdown -d /home/"$NM"/ -s /bin/bash -m "$NM"
cp /backup/Dennis/linux/administration/custom_file_backups/bash /home/"$NM"/.bashrc
cp /backup/Dennis/linux/administration/custom_file_backups/bash_profile /home/"$NM"/.bash_profile
cp /backup/Dennis/linux/wine/backup/fullbackup.wine.tar.bz2 /home/"NM"/
cp /backup/Dennis/linux/wine/e-Sword.desktop /home/"$NM"/Desktop/e-Sword
tar -jxf /home/"$NM"/fullbackup.wine.tar.bz2
rm /home/"$NM"/fullbackup.wine.tar.bz2
TITLE="give sudoer privlidges"
question
if test "$OUTCOME" = "y" ; then
useradd -G wheel "$NM"
fi
#strictly for user dp
if test "$NM" = "dp" ; then
mkdir /home/dp/docs ; mkdir /home/dp/down
cp -R /backup/Dennis/docs/OfficeAdmin /home/dp/docs/
mkdir /home/dp/docs/Estimating ; mkdir /home/dp/docs/Billing ; mkdir /home/dp/docs/Contracts
fi
chown "$NM":users -R /home/"$NM"/
Y="Do you want to add a password now"
question
if test "$OUTCOME" = "y" ; then
passwd "$NM"
fi
TITLE="Do you want to add another user?"
question
done
#add network printer
#This part needs ammending
#any suggestions???
TITLE="add a network printer"
question
if test "$OUTCOME" = "y" ; then
lpadmin -p HP_OfficeL7580 -v smb://MOM/HPOffice
lpadmin -d HP_OfficeL7580
fi
#adding colors to bash
TITLE="add colors"
question
#Thanks to woodsman at LQ
if test "$OUTCOME" = "y" ; then
echo "Adding boot message colors"
cp /backup/Dennis/linux/administration/custom_file_backups/add_colors/shell-colors /etc/shell-colors
cp /backup/Dennis/linux/administration/custom_file_backups/add_colors/update-issue /usr/local/sbin/update-issue
/usr/local/sbin/update-issue
echo "Adding color to grep"
echo "# Add color to grep" >> /etc/profile
echo "GREP_OPTIONS='--color=auto'" >> /etc/profile
echo "GREP_OPTIONS='--color=auto'" >> /etc/profile
echo "GREP_COLOR='1;32'" >> /etc/profile
echo "Adding color to man pages"
echo "# Add color for man pages" >> /etc/profile
echo "export PAGER=\"most\"">> /etc/profile
fi
#Providing the option to boot to gui.
TITLE="Do you want to boot to the gui (KDE)"
question
if test "$OUTCOME" = "y" ; then
cp /etc/inittab /etc/inittab.BAK
cat /etc/inittab | sed -e 's/id:3:initdefault:/id:4:initdefault:/' \
> /tmp/inittab
mv /tmp/inittab /etc/inittab
fi
You can edit files in place with sed using the -i option.
This is great work for a first script. Do you have previous coding experience?
I'm sure there are minor things you could change, but at a quick glance it looks really well done.
I do something similar. I have a lot of individual scripts I keep in one directory to make each change. Doing them as individual files has the advantage of allowing me to rerun them if necessary. My mkinitrd.sh being a good example of that as I have to run that each time I do a custom kernel build.
1) sed's '-i' option is what you're looking for. Here's one of mine as an example of it in action:
Code:
root@jupiter:~/changes# cat 007_lang.sh
#!/bin/bash
#
# Configure locale.
# For sh, ksh, bash
sed -i -e "s/^export LANG=en_US/export LANG=en_GB/" /etc/profile.d/lang.sh
# For csh
sed -i -e "s/^setenv LANG en_US/setenv LANG en_GB/" /etc/profile.d/lang.csh
root@jupiter:~/changes#
2) I believe that if you add your userid to the power group, you'll be able to issue shutdown without the need to resort to sudo trickery.
I just hit the button on the front myself and let ACPI do the shutdown for me, so I don't bother with this.
Nice okos, yeah it'll be a year in March since I've been using Linux. I love it, it's just so versatile. I've broken and reinstalled my os several times as well. That's just how you learn. It's great to see progress tho. Nice script man, keep it up.
1) sed's '-i' option is what you're looking for. Here's one of mine as an example of it in action:
Code:
root@jupiter:~/changes# cat 007_lang.sh
#!/bin/bash
#
# Configure locale.
# For sh, ksh, bash
sed -i -e "s/^export LANG=en_US/export LANG=en_GB/" /etc/profile.d/lang.sh
# For csh
sed -i -e "s/^setenv LANG en_US/setenv LANG en_GB/" /etc/profile.d/lang.csh
root@jupiter:~/changes#
2) I believe that if you add your userid to the power group, you'll be able to issue shutdown without the need to resort to sudo trickery.
I just hit the button on the front myself and let ACPI do the shutdown for me, so I don't bother with this.
Can't help you with no 3.
Thanks,
Regarding the -i option, I fiddled with the command but did not seem to get the syntax correctly. However, I just followed your example which worked. The following command,
sed -i "s/red/blue/" file.txt
Changed all of the red colors to blue in a sample file.text
Regarding #2 I did not see any of the /sbin/halt /sbin/reboot files in the power group.
Quote:
Originally Posted by David the H.
You can edit files in place with sed using the -i option.
This is great work for a first script. Do you have previous coding experience?
I'm sure there are minor things you could change, but at a quick glance it looks really well done.
I had learned BASIC in high school. But that was over 25 years ago.
Does anyone have suggestions regarding my 3rd question. Is there a command to search out shared printers on a network?
next be 1 of my "h" files (help file I can run, prints out help)
plclh
perl command line help [mostly a bunch of one liners] (also has find, sed, and others) (all files in a folder [might also do sub folders] is supposed to substitute x for y in files.
Code:
al@P5Q:~$ cat ~/bin/plclh
#!/usr/bin/perl
print <<STUFF;
# tr/\xA0/ /
perl -pi.bak -e 'tr/\\xA0/ /' filenames*
# all files in a folder
find YourDirectory -type f -print0 | xargs -0 sed -i 's/xxxx/yyyyy/g'
find . | xargs perl -p -i.bak -e 's/oldstring/newstring/g'
perl -e 's/string/stringier/gi' -p -i.bak *.html
via commandline, runs that command, replaces string with
stringier using g global and i insensitive options.
Operates on all .html files in a folder (CWD) also makes
a backup of each file prior to the operation.
# tested s/print/unlink/ to delete files
perl -MFile::Find -e 'find sub { print if /\\.txt\$/} , shift || "."'
# from cur wrk dir, recursively deletes the dir "dir_name" and underneath
perl -MFile::Path -e 'rmtree("dir_name")'
STUFF
# end
wireshark? -- printer don't "send out" anything -- so, how ya gonna find? -- call the GhostBusters. Ah, but, print server gets found on my LAN by my chatty arp ing 2wire Gateway/router. 2wire lists the print server as a device in its device list. 2wire I think uses arp to find LAN devices/computers
A DSL modem and a Linksys router with ddwrt or whatever open source firmware on it will soon replace the 2wire. 2wire does alright though, but heartbeat/chatty (I'm not one of them auto pilot Win users).
Maybe you need to record printers in a database, even if only a simple xml file as database and make available on your backups. Perl can blurt from xml to.
perlmeister (www search word) have available a sys admin and a blurt module (uses Perl to do what you did [and more] with your shell script).
Regarding #2 I did not see any of the /sbin/halt /sbin/reboot files in the power group.
Yes, sorry, I think I gave you a bum steer there. I'm sure I read it somewhere, but you're right it doesn't seem to do the job. I'll stick to giving 1st hand answers in future.
Yes, sorry, I think I gave you a bum steer there. I'm sure I read it somewhere, but you're right it doesn't seem to do the job. I'll stick to giving 1st hand answers in future.
GazL I think perhaps that you were thinking of the ability to shutdown the system in runlevel 4. You need to be a member of the power group to get the "Shutdown" option with the login managers.
GazL I think perhaps that you were thinking of the ability to shutdown the system in runlevel 4. You need to be a member of the power group to get the "Shutdown" option with the login managers.
Ahh, I knew I'd picked up a connection between the 'power' group and shutdown somewhere. That would explain it. Thanks for that.
1. I used the sed command to edit a file for personal use. It seems that sed will not actually edit a file. From what I understand, sed streams the file in question and replaces the desired changes into a different file. I then have to move the new file to replace the original file. This seems a little awkward. There should be a better way to go about editing a file.
This is true for certain versions of sed. SNU sed can edit the file in place with the -i option, but many other versions cannot. If you ever have any plans for working on non-GNU systems such as BSD or UNIX, I suggest you learn of other ways to manage these sed tasks. Typically this requires the use of a temporary file.
Quote:
Originally Posted by okos
Code:
#!/bin/sh
#checking who is executing this script
WHO=`whoami`
if test $WHO != "root"; then
echo "You need to be in root to execute this script!"
exit
fi
This is less than ideal. Whenever coding anything, but especially when using shell scripts, you want to simplify your tests as much as possible in order to improve performance. Here, you are assigning a variable with the output of a command, then checking this variable only once in an if-else statement. This would be best done simply by checking it inside the if-else statement.
Code:
if [ ! "$(whoami)" = "root" ]; then
This is also much cleaner. In the above example, you wouldn't notice much performance gain, but in others you could see quite a bit.
Quote:
Originally Posted by okos
Code:
#Confirming mounting usb drive.
echo "***Mounting backup files ***"
if test -e /backup = "" ; then
This is rather ugly. :-) Not only is the code difficult to read, but you're also checking to see if /backup exists. It might very well exist and be a file instead of a directory. Try this instead.
Code:
if [ -d /backup]; then
See bash(1) for more details. Look for the "CONDITIONAL EXPRESSIONS" section.
Quote:
Originally Posted by okos
Code:
#Editing /etc/sudoers for the purpose of adding shutdown group so users
#can shut off the computer.
chmod +w /etc/sudoers
This is not just dangerous, it's stupid dangerous. Do not give write permission for files like this to every user on the machine even for a limited time. Since this script only runs as root, it shouldn't be needed anyhow.
Quote:
Originally Posted by okos
Code:
echo "%wheel ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
echo "***Adding shutdown group for users to shut down***"
You should first test to see if this line is present before adding it. I can see this get added time and time again by mistake. The following should work.
Code:
if ! grep "^%wheel.*ALL=(ALL).*NOPASSWD:.*ALL" /etc/sudoers"; then
echo "%wheel ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
echo "***Adding shutdown group for users to shut down***"
fi
Note that if you know for a fact the whitespace seperating these arguments will always be spaces (as opposed to tabs or tabs and spaces, which the default Slackware sudoers file contains) you can make this more robust by using:
This has the advantage in that it will not match groups with names such as "wheeler" or "wheels_on_the_bus" and the like.
Quote:
Originally Posted by okos
Code:
if test "$OUTCOME" = "y" ; then
chmod -x /etc/rc.d/rc.inet1
installpkg /backup/Dennis/linux/wireless/wicd*.tgz
echo "ifconfig lo up" >> /etc/rc.d/rc.local
fi
A better solution would be to copy to a stock rc.inet1.conf file into place. This would remove the need to chmod -x rc.inet1 (which you generally shouldn't want to do ever) and removes the need to bring lo up in rc.local. Also, consider using upgradepkg --install-new instead of installpkg. That goes for all other instances in this script where you using installpkg. I'm only gonna point it out once though.
Consider using the "-k" option to useradd to specify a skeleton directory instead of using these individual copy operations. "-k" will copy all the contents of the directory specified into the new user's home directory for you.
GazL I think perhaps that you were thinking of the ability to shutdown the system in runlevel 4. You need to be a member of the power group to get the "Shutdown" option with the login managers.
This is untested (I'm not in a position where I can test it right now), but you can likely get the same behavior from command line by doing this:
Thanks for the great response. I appreciate the input.
Quote:
Originally Posted by +Alan Hicks+
This is true for certain versions of sed. SNU sed can edit the file in place with the -i option, but many other versions cannot. If you ever have any plans for working on non-GNU systems such as BSD or UNIX, I suggest you learn of other ways to manage these sed tasks. Typically this requires the use of a temporary file.
So perhaps it is best to use sed to >a temp file then mv the file to its proper location as I had previously done
Quote:
Code:
if [ ! "$(whoami)" = "root" ]; then
This is also much cleaner. In the above example, you wouldn't notice much performance gain, but in others you could see quite a bit.
Could you explain the coding for this, ! "$(whoami)" ?
Quote:
This is not just dangerous, it's stupid dangerous. Do not give write permission for files like this to every user on the machine even for a limited time. Since this script only runs as root, it shouldn't be needed anyhow.
As I recall, root did not have write permission either.
Perhaps chmod 700 /etc/sudoers would be a better option.
Quote:
A better solution would be to copy to a stock rc.inet1.conf file into place. This would remove the need to chmod -x rc.inet1 (which you generally shouldn't want to do ever) and removes the need to bring lo up in rc.local.
I see your point. However, what I wanted was the option to use the rc.inet1.conf that I had customized for my system. I had set it up so that wired would be option (0) and wireless is option (1) in the rc.inet1.conf. But what I found was when I booted, my computer would first obtain an IP address first from my wired connection and secondly from my wireless. It took much longer to boot. What I really wanted to do was have my computer first try to connect wired. If there was no wired connection, then and only then would it try my encrypted wireless network. And finally, unencrypted networks. As a result,I decided to go with wicd instead.
Quote:
Also, consider using upgradepkg --install-new instead of installpkg. That goes for all other instances in this script where you using installpkg. I'm only gonna point it out once though.
Great idea.
Quote:
Consider using the "-k" option to useradd to specify a skeleton directory instead of using these individual copy operations. "-k" will copy all the contents of the directory specified into the new user's home directory for you.
I could not find the -k option in man useradd.
Could you further explain? Possibly give an example?
Regarding
Quote:
if [ -d /backup]; then
Perhaps a better option would be
Quote:
if [ -d /backup/Dennis]; then
That will show if my usb drive is properly mounted.
Thanks again
okos
One last question, as in my script, I used lpadmin to add my wife's xp printer. I know in cups you can search for printers. Is there any script to do the same. Search out computers and then printers.
Thank you a lot. I've really to try the "color maker" of this beauty script. Any way, it does things I wouldn't do, for example...Adding the normal user to shutdown or reboot PC is very a dangerous thing . I'll use it when I'll install slackware again. Thank you again and take in consideration all that testers say. Sometimes it is not considered by programmer, and this is the most important thing of their job. Thank you and congratulations.
Thank you a lot. I've really to try the "color maker" of this beauty script. Any way, it does things I wouldn't do, for example...Adding the normal user to shutdown or reboot PC is very a dangerous thing . I'll use it when I'll install slackware again. Thank you again and take in consideration all that testers say. Sometimes it is not considered by programmer, and this is the most important thing of their job. Thank you and congratulations.
Regarding shutdown, I installed slack on my kids computer.
I want each of them the ability of turning off the computer themselves. I did not want to have to be called to turn off the computer for them. Runlevel 3 requires a little linux knowledge in the first place. If they are able to log in under runlevel 3. Then they should be mature enough to turn the computer off.
Furthermore, their computer has no network / internet access. So nobody can mess with their computer without having physical access.
I set it up on my own computer. However, I am the only user. Only I can gain access in the first place. In addition, I disabled the server option for startx so I think that would be a good security feature with regard to turning off the computer.
Regarding using the script, you will need to personalize it for your needs. The script as it is will not work on other slackware computers unless you have the same setup as I have.
However, if you do not have a usb drive to backup your info, you could edit the script for cdrom or a separate partition designated strictly for backup purposes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.