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.
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
Quote:
This has the opposite effect.
If you are a user it will print
you are in root.
If you are in root, it prints nothing.
Not sue what your point is here. Alan Hicks gave the original code snippet to demonstrate not using a temporary result variable as you originally did. The "!" negates the test as you probably know. The example he gave was this :
Code:
if [ ! "$(whoami)" = "root" ]; then
to say "if you are not root then do this". Personally I always find the code more readable like this :
Any ways, how do you write an if script to search for SHELL=/bin/bash in /etc/default/useradd
I tried different variations of the following:
Quote:
if `grep -e "SHELL=\/bin\/bash" /etc/default/useradd` = ""; then
echo "SHELL=\/bin\/bash" >> /etc/default/useradd
fi
When I try this it prints SHELL=/bin/bash instead of continuing the script. I do not want to print the results. Just make sure that SHELL=/bin/bash doesn't already exist.
When I tried other variations, I get the error
No such directory.
Im thinking it is looking for a directory /bin/bash
if [ ! -f /etc/default/useradd ] || [ ! $(fgrep -q SHELL=/bin/bash /etc/default/useradd) ]; then
echo 'SHELL=/bin/bash' >> /etc/default/useradd
fi
In English
If the file /etc/default/useradd does not exit OR does not contain the string "SHELL=/bin/bash"
append the string
A couple of pointers:
use [] for test.
use $() in place of back-ticks "``"
You can use "&&" "||" between tests for AND and OR.
fgrep will run faster and will take your string literally so no need to escape characters (putting '' around the string is good practice to stop the shell from interpreting things)
Checking that a file exists is usually a good idea before running a test against it.
Thank you sooo much for your help.
mRgOBLIN, you not only gave the answer, but an explanation behind your answer.
That is just what I needed.
I will give it a try and check the results.
You should really check the output of the mount command for that. The directory can exist either way (and if it's the base directory, it *will* exist either way).
I want to thank all of you for your input. It was very helpful.
I found quite a few bugs and had to re-run the script several times. Mostly the problems were typos. I installed a stripped down version of slack 12.2 on a small partition to test the script. I did not want to use any type of virtual viewer because it tends to run rather slow.
Also, I added some additional conditional statements so I did not have to re-install things over and over each time I tested the script.
As someone who is new to writing scripts, I had some difficulty with using the correct characters:
Do I use `` or '' or "". Should I use "$N" or $N. Or when to use \ to show a character as it is other then its function. For example \$N prints the dollar sign before the N. While $N prints the variable. I had additional trouble with [] or {}, or (). The syntax is not exactly self explanatory. I learned much of this from the rute book, or googling. I have not yet read other bash scripting tutorials.
So... What do you think
Quote:
#!/bin/sh
#Because I break my system so many times. 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 tend to forget things so gave simple explinations so I will remember.
#It should also help other noobs like me to salvage parts for their own purposes.
#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
}
echo "=========================================================================="
echo "=====Some parts may take a while. Please stand by.========================"
echo "=========================================================================="
#checking who is executing this script
#if whoami does not equal root then
if [ ! "$(whoami)" = "root" ]; then
echo "You need to be in root to execute this script!"
exit
fi
#Confirming mounting usb drive.
echo "***Mounting backup files ***"
# if directory /backup does not exists then
if [ ! "$(ls / | grep backup)" = "backup" ] ; then
mkdir /backup
fi
# get device info
MYDISK=`fdisk -l | grep 11918 | grep -o '/dev/sd..'`
#test to see if disk exists.
if test "$MYDISK" = "" ; then
echo "***You need to connect usb WD 1TB drive to run this script***"
exit
fi
# unmount the default mount of /media/disk or /media/disk-1
umount "$MYDISK"
mount "$MYDISK" /backup
echo "Installing essential dellfand"
echo "This may take a while. Please stand by."
# dellfand prevents over heating of the cpu
FAN="$(find /usr -d -name dellfand-0.9)"
if [ ! "$(expr $FAN | grep -o dellfand)" = "dellfand" ] ; then
cp /backup/Dennis/linux/tar/* /usr/local/src
# I seemed to have trouble extracting tar files with tar -xvf <path> dellfand-0.9.tar.bz2
# So I changed directory first.
cd /usr/local/src/ ; tar -xvf dellfand-0.9.tar.bz2
rm dellfand-0.9.tar.bz2y
cd /usr/local/src/dellfand-0.9 ; make
./dellfand 1 10 38 39 45
#Editing /etc/sudoers for the purpose of adding shutdown group so users
#can shut off the computer.
chmod 740 /etc/sudoers
# if %wheel.*ALL=(ALL).*NOPASSWD:.*ALL does not start a line then
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
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 network scripts and install WICD"
question
if test "$OUTCOME" = "y" ; then
chmod -x /etc/rc.d/rc.inet1
upgradepkg --install-new /backup/Dennis/linux/wireless/wicd*.tgz
echo "ifconfig lo up" >> /etc/rc.d/rc.local
fi
#Instally personal packages from usb hdd
TITLE="install personal packages"
question
if test "$OUTCOME" = "y" ; then
upgradepkg --install-new /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
upgradepkg --install-new /backup/Dennis/linux/games/*.tgz
fi
TITLE="add a user"
question
if test "$OUTCOME" = "y" ; then
if [ ! -d /etc/default ] ; then
mkdir /etc/default
fi
if [ ! -d /etc/skel ] ; then
mkdir /etc/skel
fi
# Making sure the skeleton dircectory is properly setup to add several users see (man useradd)
if [ ! -f /etc/default/useradd ] ; then
echo "expr useradd -D" > /etc/default/useradd
cat /etc/default/useradd | sed -e 's/SHELL=/SHELL=\/bin\/bash/g' \
> /tmp/useradd
mv /tmp/useradd /etc/default/useradd
echo "SKEL=/etc/skel" >> /etc/default/useradd
elif [ ! $(fgrep -q SHELL=/bin/bash /etc/default/useradd) ]; then
cat /etc/default/useradd | sed -e 's/SHELL=/SHELL=\/bin\/bash/g' \
> /tmp/useradd
mv /tmp/useradd /etc/default/useradd
echo "SKEL=/etc/skel" >> /etc/default/useradd
elif [ ! $(fgrep -q SKEL=/etc/skel /etc/default/useradd) ]; then
echo "SKEL=/etc/skel" >> /etc/default/useradd
fi
#Filling up /etc/skel for default additions to each user.
if [ ! -f /etc/skel/.bashrc ]; then
cp /backup/Dennis/linux/administration/custom_file_backups/bash /etc/skel/.bashrc
cp /backup/Dennis/linux/administration/custom_file_backups/bash_profile /etc/skel/.bash_profile
cp -R /backup/Dennis/linux/wine/Desktop /etc/skel
cp /backup/Dennis/linux/wine/backup/fullbackup.wine.tar.bz2 /etc/skel/
cd /etc/skel ; tar -jxvf fullbackup.wine.tar.bz2
rm /etc/skel/fullbackup.wine.tar.bz2
fi
fi
while test "$OUTCOME" = "y" ; do
#***********************adding users************************
echo "Add a user name";read NM
if [ ! -d /home/$NM ]; then
useradd -k /etc/skel -g users -G slocate,disk,cdrom,plugdev,sys,lp,shutdown \
-d /home/"$NM"/ -m "$NM"
#strictly for user dp
if test "$NM" = "dp" ; then
mkdir /home/dp/{docs,down}
cp -R /backup/Dennis/docs/OfficeAdmin /home/dp/docs/
mkdir /home/dp/docs/{Estimating,Billing,Contracts}
fi
TITLE="give sudoer privlidges"
question
if test "$OUTCOME" = "y" ; then
usermod -G wheel "$NM"
echo "sudoer privlidges given"
fi
chown "$NM":users -R /home/"$NM"/
else
echo "User name exists you need to select a different user"
fi
TITLE="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 http://humanreadable.nfshost.com/sdeg/index.htm
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/
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="boot to the gui (KDE)"
question
if test "$OUTCOME" = "y" ; then
cp /etc/inittab /etc/inittab.BAK
sed -i "s/id:3:initdefault:/id:4:initdefault:/" /etc/inittab
fi
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
Just a quick point - and please don't think I'm getting at you here - please when posting a long piece such as your script use "[CODE]" tags rather than "[QUOTE]" as otherwise the entire script appears in the post without scroll bars.
if [ ! "$(ls / | grep backup)" = "backup" ] ; then
mkdir /backup
fi
I expect this will fail unless you used
Code:
if [ ! $(ls -1 / |grep backup = "backup/") ]
In this particular context
Code:
if [ ! -d /backup ]; then
is a much more reliable and simple solution. (just remember you always need a space after "[" and before "]" )
Quote:
Do I use `` or '' or "". Should I use "$N" or $N. Or when to use \ to show a character as it is other then its function. For example \$N prints the dollar sign before the N. While $N prints the variable. I had additional trouble with [] or {}, or ().
There are usually so many exceptions to rules that I won't go into too great a detail but use this a general guide.
The use of `` (backticks) is pretty much depreciated in favour of $(some action) so forget them.
Use '' (single quotes) around anything that you want the shell to interpret literally. e.g.
As you have noticed you can also make some parts literal by escaping special characters with a \
Code:
mrgoblin@proteus:~> echo "\$foo"
$foo
mrgoblin@proteus:~> echo '\$foo'
\$foo
mrgoblin@proteus:~> echo "\$foo has the value $foo"
$foo has the value result
It's also good practice to use {} with variables like ${variable}
It lets the shell know where the variable name ends.
Code:
mrgoblin@proteus:~> echo "$foos can be unexpected"
can be unexpected
mrgoblin@proteus:~> echo "${foo}s can be unexpected"
results can be unexpected
Really you should read some of the bash tutorials but everything takes time.
I think you are going about the right way to learn by having a goal that you want to achieve... it will keep you focused.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.