LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   mkdir in bash script - command substitution (https://www.linuxquestions.org/questions/linux-newbie-8/mkdir-in-bash-script-command-substitution-816459/)

casperdaghost 06-26-2010 12:17 AM

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.

grail 06-26-2010 12:36 AM

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"???

murugesan 06-26-2010 01:40 AM

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

smoker 06-26-2010 05:10 AM

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.


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