LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 03-03-2009, 01:58 PM   #16
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 38

FYI

Quote:
if [ ! "$(whoami)" = "root" ]; then echo "you are in root" ; fi
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.
 
Old 03-03-2009, 02:17 PM   #17
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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 :
Code:
if [ "$(whoami)" != "root" ]; then
but it's a trivial change.
 
Old 03-03-2009, 06:13 PM   #18
acummings
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 615

Rep: Reputation: 50
Code:
if [ ! "$(whoami)" = "root" ]; then
Code:
if [ ! "$(whoami)" = "root" ]; then echo "you are not in root -- you must logon as root" && exit; fi
IOW, root gets nothing echoed to screen.

Only non root gets the echo.

Is that the way you wanted it?

I guessed at the && exit part (my intent to exit the script so can logon as root

Alan.
 
Old 03-03-2009, 06:26 PM   #19
mRgOBLIN
Slackware Contributor
 
Registered: Jun 2002
Location: New Zealand
Distribution: Slackware
Posts: 999

Rep: Reputation: 231Reputation: 231Reputation: 231
I think you need to explain that the "!" in there negates the test so in human terms.

Code:
[ ! "$(whoami)" = "root" ]
if the result of whoami does not equal the string "root" then return true
Code:
[ "$(whoami)" = "root" ]
if the result of whoami equals the string root then return is true

As previously mentioned != is more readable but still requires you to understand the "!" which is what I think the OP missed.
 
Old 03-03-2009, 11:38 PM   #20
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 38
I have another question. I am stumped
Ive been fiddling with this for two days now.

As suggested by +Alan Hicks+ post #11 use the useradd -k option to setup many users. I think is a great idea.

I found some great help from woodsman here.

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

Any suggestions?

Last edited by okos; 03-03-2009 at 11:39 PM.
 
Old 03-04-2009, 02:27 AM   #21
mRgOBLIN
Slackware Contributor
 
Registered: Jun 2002
Location: New Zealand
Distribution: Slackware
Posts: 999

Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
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.
 
Old 03-04-2009, 11:28 AM   #22
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 38
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.

Last edited by okos; 03-04-2009 at 11:32 AM.
 
Old 03-06-2009, 01:56 PM   #23
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 38
FYI
Quote:
Originally Posted by +Alan Hicks+ View Post

Code:
if [ -d /backup]; then
This does not work I get the following error
line 13: [-d: command not found

I went with a different option

Quote:
if [ ! "$(ls / | grep backup)" = "backup" ] ; then
mkdir /backup
fi
But thanks for the help anyways.
 
Old 03-06-2009, 02:47 PM   #24
mRgOBLIN
Slackware Contributor
 
Registered: Jun 2002
Location: New Zealand
Distribution: Slackware
Posts: 999

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by okos View Post
FYI

This does not work I get the following error
line 13: [-d: command not found
It would have worked it was just missing a space before the last ]

Code:
if [ -d /backup ]; then
 
Old 03-06-2009, 04:20 PM   #25
rworkman
Slackware Contributor
 
Registered: Oct 2004
Location: Tuscaloosa, Alabama (USA)
Distribution: Slackware
Posts: 2,559

Rep: Reputation: 1351Reputation: 1351Reputation: 1351Reputation: 1351Reputation: 1351Reputation: 1351Reputation: 1351Reputation: 1351Reputation: 1351Reputation: 1351
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).
 
Old 03-08-2009, 03:00 PM   #26
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 38
Thumbs up My final version. I think.....

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

else
cd $FAN ; ./dellfand 1 10 38 39 45
fi
chmod 550 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/cups/printers.conf /etc/cups/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 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

Last edited by okos; 03-08-2009 at 03:14 PM.
 
Old 03-08-2009, 07:31 PM   #27
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 03-08-2009, 08:57 PM   #28
mRgOBLIN
Slackware Contributor
 
Registered: Jun 2002
Location: New Zealand
Distribution: Slackware
Posts: 999

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Code:
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.
Code:
mrgoblin@proteus:~> foo=result
mrgoblin@proteus:~> echo '$foo'
$foo
mrgoblin@proteus:~> echo "$foo"
result
mrgoblin@proteus:~>
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.

Last edited by mRgOBLIN; 03-08-2009 at 09:01 PM.
 
Old 03-11-2009, 12:03 AM   #29
rkrishna
Member
 
Registered: Mar 2005
Location: chennai(madras), India
Distribution: slackware ofcourse
Posts: 654

Rep: Reputation: 32
Quote:
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.
i press my power button to power off my machine.
 
Old 03-12-2009, 01:34 AM   #30
alMubarmij
Member
 
Registered: Dec 2005
Posts: 140
Blog Entries: 1

Rep: Reputation: 16
Good script, what you call it ?
I hope you make mare.
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
DCOPserver, yea again, WHY??? joewee SUSE / openSUSE 0 10-28-2006 11:46 AM
Oh Yea, Slack 9 -Juke Box 320mb Slackware 0 09-03-2003 07:30 PM
o yea and disk druid Spaz17 Linux - Newbie 1 07-04-2003 10:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

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