BaSH: another problem with script

Jun 10, 2004
31
0
0
Yep yep, it's me again!

In my kick-ass script *roll eyes*, I want to do a tarball backup of a user's home directory and then delete the user's account and the user's home directory, leaving a tarball backup of the deleted directory in /root/users_deleted.

Here's the part of the script:

echo -n "Removing $2..."
if cat /etc/passwd | grep "^$2:" > /dev/null; then
tar -czf /root/users_deleted/$2_'date +"%Hh%M_%d-%m-%Y"'.tgz /home/$2
if [ $? == 0 ]; then
$userdel $2
if cat /etc/passwd | grep "^$2:" > /dev/null; then
echo " Done."
exit 0
else
echo " ERROR: Could not delete account."
exit 1
fi
else
echo " ERROR: Could not create a backup of $2's files."
exit 1
fi
else
echo " ERROR: $2 does not exist."
exit 1
fi

With this version of the script, it does create the backup file (even though the date doesn't show correctly, it prints date +"%Hh%M_%d-%m-%Y" as the filename (after $2_ of course).

The problem with the script is that it always give me the error that it couldn't create a backup of the user's file. I guess it isn't working mainly because of the bad filename of the tarball..?


Thanks, Frank
 

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
Are you using ` or ' for the date command? (the first is under the tilde, the second is the single quote)
 
Jun 10, 2004
31
0
0
Ooops, was wrong, I used `, not single quotes.

If I remember, I tried modifying the script to try it with `s, 's and "s.

Which one should I use for date, so I could work on the problem while being sure that I used the right quotation..?


Thanks, Frank
 
Jun 10, 2004
31
0
0
I worked some time on it and now it works better, it says, as the error:

Removing blablabla...tar: Removing leading `/' from member names
./setup_user: line 56: blablabla: command not found
Done.

Looks like it performs $2 as a command?!?!?!?

BTW: As soon as this part works, I'll > /dev/null the output of tar to hide the Removing leading`/` from member names.. it's quite ugly :p


Thanks, Frank
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
#!/bin/bash

tar -cvf /tmp/$1.tar /home/$1
gzip /tmp/$1.tar
mv /tmp/$1.tar.gz /root/users_deleted/$1.`date +%d%m%Y`.tar.gz
rmuser $1

:p

$2 would be the third thing you type in:
rm stuff.txt menu.pdf
menu.pdf is $2, stuff.txt is $1, rm is $0.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Backticks (`) run the contained text as a command, and return the text. So this:

echo "hi"

is the same as:

echo `echo "hi"`
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
To remember the differences between the qoutes just keep this script around and run it time to time. I do stuff like this to remind myself of different -isms of stuff incase I forget it.

#! /bin/bash

TEST="ls /"

echo "This is the test"
echo " "
echo " "
echo "This is what happens when you use double qoutes. \" \""
echo "$TEST"
echo " "
echo "This is what happens when you use single qoutes. \' \'"
echo '$TEST'
echo " "
echo "This is what happens when you use backticks. \` \`"
echo `$TEST`
echo " "
echo " "
echo "See also what this looks like:"
echo "double qoutes"
echo "hello \" \""
echo " "
echo "single qoutes"
echo 'hello \" \"'
echo " "
echo "but look at the back tick. we get a error:"
echo `hello \" \"`
echo " "
echo " "
echo "Variations"
echo "This is a third type. By using ( ) you can create a new bash enviroment temperarially for whatever reason"
pwd
echo " "
(cd /etc; pwd; cd /var/; pwd; cd /usr/bin/ ; pwd)
echo "but we never went anywere:"
pwd
echo " "
 
Jun 10, 2004
31
0
0
Ah!

Thanks all for the answers, I got everything working properly now. The only problem is the /dev/null'ing of some commands that doesn't work.. :S

Is there any way of "muting" a command by using quotes? > /dev/null isn't working on every command that outputs something.


Thanks, Frank
 

aspitzer

Member
Jul 9, 2001
187
0
0
"> /dev/null" only sends stdout to /dev/null, anytime you have errors, or something that writes to stderr, it will still appear on the screen, as in:

rm -f /etc > /dev/null
rm: cannot remove `/etc': Permission denied

vs

rm -f /etc > /dev/null 2>&1

the 2>&1 says to also send the stderr to stdout, which then goes to /dev/null
 

aspitzer

Member
Jul 9, 2001
187
0
0
[aspitzer@oscar aspitzer]$ rm -f /etc 2>/dev/null
[aspitzer@oscar aspitzer]$ rm -f /etc > /dev/null 2>&1
[aspitzer@oscar aspitzer]$ rm -f /etc > /dev/null
rm: cannot remove `/etc': Permission denied

looks like it!
 
Jun 10, 2004
31
0
0
I did a few tests and it pointed me out that 2> /dev/null is not the same as > /dev/null 2>&1. From my tests, I think that the difference is that the first one only hides the error messages and the second one hides the error messages AND the normal stdout messages.


Frank
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Is 2> /dev/null

.. sends stderr to /dev/null, which means that you won't see any error messages, but you'll see normal output (stdout).

ls > /dev/null 2>&1

.. sends stdout to /dev/null, which means you won't see normal output. 2>&1 sends stderr to stdout, and since stdout is being redirected to /dev/null, that means that stderr will go there as well. You won't see any output.

(oh, 1 is stdout, 2 is stderr. stdout is "standard output," for normal text output. stderr is "standard error," which is for errors. 0 is stdin, and that's used for input instead of output. When you type text into a prompt, that text is generally sent to stdin. These are the three "standard" file descriptors.)