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. |
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"??? |
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 |
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); Code:
logfile=$(/bin/mkdir -p /tmp/pw_log/`date +20%y%m%d_%H%M%S`.log); If you insist on creating a directory do something like this : Code:
#!/bin/bash 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 If you want to specify the directory to list on the command line use this : Code:
#!/bin/bash Code:
~/logtest.sh /var/log <username> is whoever is running the script. |
All times are GMT -5. The time now is 09:31 PM. |