LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 06-26-2010, 12:17 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 346

Rep: Reputation: 16
mkdir in bash script - command substitution


I was to set up a cronjob that sends its output to a logfile, i want that log file to be unique. the easiest way to set this up would be to make the directory in the script, then output the command to that directory, they call that directory via is full path in cron (because cron only uses full path names.

#!/bin/bash
$logfile=$(/bin/mkdir -p /tmp/pw_log/`date +20%y%m%d_%H%M%S`.log);
ls -ltr > $logfile;

this does not work

keep in mind the command "/bin/mkdir -p /tmp/pw_log/`date +20%y%m%d_%H%M%S`.log" works fine on the command line - but once I put it in the shell, it does not work.
 
Old 06-26-2010, 12:36 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,577

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
So let's try this, run your command on the command line and tel us what is the exact output after running the command?

Oh and $logfile= ... so you are trying to create a variable called "$logfile"???
 
Old 06-26-2010, 01:40 AM   #3
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
Change your script as:

#!/bin/ksh
if [ ! -d /tmp/murugesan_nagarajan/ ]
then
mkdir -p /tmp/murugesan_nagarajan > ./delete.txt 2>&1
if [ $? -eq 0 ]
then
echo Created the directory /tmp/murugesan_nagarajan/
else
if [ -f ./delete.txt ]
then
echo Error on creating the directory :/tmp/murugesan_nagarajan:
cat ./delete.txt
rm -f ./delete.txt
else
echo Unable to create the following file ./delete.txt in current directory
echo Error on creating the directory :/tmp/murugesan_nagarajan:
mkdir -p /tmp/murugesan_nagarajan
fi
fi
else
echo The directory /tmp/murugesan_nagarajan/ exists
fi
logfile=/tmp/murugesan_nagarajan/`date +20%y"_"%m"_"%d"_"%H"_"%M"_"%S`.log;
ls -ltr 2>./delete.txt > $logfile
if [ $? -eq 0 ]
then
echo "The output \"ls -ltr\" is written to the following file:$logfile"
else
echo Unable to write the output of \"ls -ltr\" to the following file $logfile
if [ -f ./delete.txt ]
then
cat ./delete.txt
rm -f ./delete.txt
else
ls -ltr > $logfile
fi
fi



http://geocities.ws/murugesan/techni...g/if_else.html
 
Old 06-26-2010, 05:10 AM   #4
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
If you can make a unique directory, then you can make a unique log file. Why make a directory and complicate things ? You are already using the date and you should not assign a variable using $variable.
Code:
$logfile=$(/bin/mkdir -p /tmp/pw_log/`date +20%y%m%d_%H%M%S`.log);
should be more like
Code:
logfile=$(/bin/mkdir -p /tmp/pw_log/`date +20%y%m%d_%H%M%S`.log);
But this causes errors when you try to write to it because you haven't specified a filename only the directory.

If you insist on creating a directory do something like this :
Code:
#!/bin/bash

basedir="/tmp/pw_log/"
logdir=$(date +%Y%m%d_%H%M%S)
/bin/mkdir -p $basedir$logdir
ls -ltr >$basedir$logdir/log.log
This will of course only list the contents of the directory the script is run from.
You also don't need semi-colons at the end of each line, and why use 20%y when %Y does the job for you ?

Do it without the directory and it looks like :
Code:
#!/bin/bash

basedir="/tmp/pw_log/"
logfile=$(date +%Y%m%d_%H%M%S)
ls -ltr / >$basedir$logfile.log
This lists the contents of the root directory.
If you want to specify the directory to list on the command line use this :

Code:
#!/bin/bash

basedir="/tmp/pw_log/"
logfile=$(date +%Y%m%d_%H%M%S)
ls -ltr $1 >$basedir$logfile.log
Which you call like this :
Code:
~/logtest.sh /var/log
This runs the script named logtest.sh located in /home/<username> with the specified directory to list being /var/log
<username> is whoever is running the script.

Last edited by smoker; 06-26-2010 at 05:55 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash: case and command substitution--what gives? David the H. Programming 9 09-03-2009 02:08 AM
bash script command substitution and quoting brian4xp Linux - Software 8 02-05-2008 11:43 AM
BASH command substitution that starts with a pipe | Kristofer Programming 4 02-27-2007 05:52 PM
Bash Command Substitution dakensta Programming 5 11-30-2006 03:10 PM
Varible substitution in Bash script RonV Linux - Newbie 2 06-04-2006 12:11 PM


All times are GMT -5. The time now is 02:09 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration