Syntax error only when command is run from cron - unix

This command:
/usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`date +%Y%m%d`".sql"
works fine from the command line but whenb cron runs it I get
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
The command is all on one line in the crontab as well so I'm confused by the line 0 and line 1 references...
Can anyone advise me as to what I am doing wrong there?

It's the obvious dumb question, but do you have the matching backquote in your crontab (crontab -l)?
The line one, line zero stuff isn't referring to the lines in the crontab, only to the 'lines' in the one-line script.
Updated:
Ah, I think I've got it. This is from crontab(5):
Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the
first % will be sent to the command as standard input.
So the percent characters in your date spec are being interpreted as newlines, which means the backquote isn't terminated before the newline, which would produce your error message.
So escape the percent characters. I'd forgotten that about crontab....

The easiest fix is probably to put the whole command in a shell script and just have that be run. So make a scriptName.sh file that contains the command you listed and have crontab call that script. That gets around all these odd problems.

Commands executed from cron do not have access to the environment variables from your login shell, including the path. So try the following (adding fully qualified path to date):
/usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`/usr/bin/date +%Y%m%d`".sql"
Of course, verify if your date command is located elsewhere by running which date then adjust the path if necessary.

Related

sh '<' syntax unexpected error

I am struggling with running new asp.net 5 on my qnap box. As far as I understand it is a strongly modified version of debian.
As part of running installation script I got this error:
-sh: /root/.dnx/dnvm/dnvm.sh: line 616: syntax error near unexpected token `<'
-sh: /root/.dnx/dnvm/dnvm.sh: line 616: read versionOrAlias downloadUrl < <(__dnvm_find_latest "$runtime" "$arch" "$os")'
I run my script bu using script command like:
script /root/.dnx/dnvm/dnvm.sh
as stated in documentation and previous installation script.
By commenting this line out I was able to run whole script but obviously dnvm command does not work properly.
My question is:
What does it do (line with < < syntax) and how do I fix it or rewrite so that my qnap box unix can understand it.
<(...) is Process substitution. /bin/sh doesn't support it, but /bin/bash does. Try changing the shell.

Sqlite Mode Error When Running Script with Output Redirection

I have the following shell script
cat <<EOF | sqlite3 /path/to/my/db.sqlite
.mode line;
select item from mytable;
EOF
When running the script with output redirection, I get this error:
Error: mode should be one of: column csv html insert line list tabs tcl
This happens also when I try different modes. When I run the script without output redirection (printing to terminal), it works fine. What's going on?
Figured it out-- actually the same error was being thrown without output redirection, but I was missing it because normal output was being dumped to the screen instead of a file. The problem in my script is that there should be no ; after mode line.

Executing console command in R

I would like to execute this DOS command under R:
iconv -f ISO-8859-1 -t UTF-8 FileName.md > FileNameNew.md
The above command creates new file after transforming from ISO to UTF.
I have tried execute this command however unsuccessfully with:
system(paste("iconv -f ISO-8859-1 -t UTF-8 FileName.md > FileNameNew.md", sep=""))
This gives me two types of errors:
Invalid argument
No such file or directory
I don't think the issue is the second since when I run the command under R it in fact executes the command as it re-reads the FileName.md, which means he found the file. I think it is just a issue with the > and hence formulation of the command in the system(paste("")) command.
When I rund this command directly under console it works.
The problem is (most likely) simply with where the R session is located. Check this by running getwd() in R and see if it is in the same place as the file. The paste part shouldn't be needed, as it is not really pasting anything (paste combines 2 strings together, while it is one string here).
Solve this by explicitly attaching input and output to those files.
If you would insist on using paste, you could use it for instance like this:
system(paste("iconv -f ISO-8859-1 -t UTF-8 ", getwd(), "/FileName.md > ",
getwd(), "/FileNameNew.md", sep=""))

Running date +%s command from a file

When I run the command date +%s directly on unix terminal, it correctly executes and gives number of seconds since 1970.
However, when the same command is in a script file, say temp.sh as below:
business_dt=date +%s
echo $business_dt
On executing above script, it throws error as below:
-ksh: +%s: not found [No such file or directory]
How to resolve this issue?
You need to use command substitution to assign the output of a command to a variable. The syntax is var=$(command). So, try changing your script to:
business_dt=$(date +%s)

Redirect output to one file and errors to another in Unix

Let's say I have a command called "enjoy." I'm expecting enjoy to give valid output and an error message. How do I call enjoy such that the valid output goes to one file and the error messages go to another file?
enjoy > log.txt 2> errors.txt
Assuming of course that you've used STDOUT and STDERR properly and you're using a nice shell. If you're using csh, you need to do something more complicated:
(enjoy > log.txt) >& errors.txt
This works because >& redirects both STDOUT and STDERR - but STDOUT has already been redirected. The parentheses make sure that STDOUT is long gone before the data gets anywhere near the overzealous >&.

Resources