In the commandline I am able to do
cp htlog.out test.$(date '+%m%d%Y')
but when I try to crontab it like this:
37 17 * * 1-5 cd the/dir && cp htlog.out test.$(date "+%m%d%Y")
It fails with the following message from the CRON DAEMON:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
And I do have endlines after the command. What is wrong with that crontab entry?
% in crontab have a special meaning, it should be backslashed, so:
37 17 * * 1-5 cd the/dir && cp htlog.out test.$(date "+\%m\%d\%Y")
From man 5 crontab :
A "%" character in the command, unless escaped with a backslash
* (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
Related
I am trying to write a script to output lines which fulfill a certain criteria into a new .txt file, trying to combined unix and awk
been googling but keep getting this error:syntax error near unexpected token `done'
Filename="bishan"
file="659.A"
while IFS= read line
do
cat $Filename.txt | awk '{ otherSubNo = substr($0,73,100);gsub(/
/,"",otherSubNo); if(length(otherSubNo)>8){ print "Subscriber Number is
",": ",substr($0,1,20)," Other Subscriber Number is ", " :
",substr($0,73,100) }}'| wc -l >> $Filename.txt
done <"$file"
example of 659.A is as follows:
This is the first line of the 659.a file:
6581264562 201611050021000000002239442239460000000019010000010081866368
00C0525016104677451 100C 0 0000
0111000 000000000000000000006598540021 01010000000000659619778001010000
000000659854000300000000000000000000 004700001
Please help, I have been googling about this but no avail
I was able to reproduce the specified error, albeit only with close approximation, by typing the script in notepad (windows) and testing it in cygwin.
script.sh:
while read myline
do
echo $myline
done
In ksh:
~> /usr/bin/ksh ./script.sh
: not found
./script.sh[7]: syntax error: 'done' unexpected
In bash:
~> /usr/bin/bash ./script.sh
./script.sh: line 2: $'\r': command not found
./script.sh: line 6: syntax error near unexpected token `done'
./script.sh: line 6: `done'
The said error (at least, in my case) is because of the CRLF characters. When I copy-paste the code to cygwin, the CRLF turns to LF (along with all invisible control characters that get lost), thus making the error disappear.
I have been trying all day to find a way to run this line (which works in bash) in R, and I keep getting errors about the round brackets... I understand that the paste command gets confused when dealing with brackets, but I have tried escaping the brackets, putting them in double quotes like this "')'" but nothing works so I am out of resources. Does anybody have any idea how this could work in R?
system(paste("sortBed -i <(awk -v a=1 -v b=2 -v c=3 -v d=4 '{OFS=FS=\"\t\"} {if ($d < 0.5) print \"value\"$a, $b-\"'$d'\", $c+\"'$d'\"}' file.in > file.out", sep=""))
sh: -c: line 0: syntax error near unexpected token `('
The reason seems to be that the R system() command calls the bourne shell (sh) instead of the bourne again shell (bash). For example, the command
> system("paste <(echo 'Hi')")
will fail, mentioning the bourne shell in the process:
sh: -c: line 0: syntax error near unexpected token `('
One solution is to print the command in the bourne shell and pipe the output into bash:
> system("echo \"paste <(echo 'Hi')\" | bash")
Hi
I get the same error as you when running the line from R. As far as I can see there's missing a final parenthesis for the output process substitution in the bash script but adding that doesn't prevent the error. Also the tabulator should be double-escaped to make sure the backslash is passed onto the awk script.
One solution that we found out works in this case is to pipe the output from awk directly into sortBed.
system(paste("awk -v a=1 -v b=2 -v c=3 -v d=4 '{OFS=FS=\"\\t\"} {if ($d < 0.5) print \"value\"$a, $b-\"'$d'\", $c+\"'$d'\"}' file.in | sortBed -i", sep=""))
We didn't really get the output process substitution to work, so if anyone has any suggestions for that it would be nice to hear.
using
system(paste("wc -l file_1.txt"))
in R to obtain the line number of a file
The output is
1601 file_1.txt
My problem is that if I type
system(paste("wc -l file_1.txt"))->kt
and then
kt
[1] 0
I would need to be able to say whether
system(paste("wc -l file_1.txt"))->kt
kt[1]==1600
or not..but I cant access the elements from the system commadn or the printout...how can i do that to somehow check whether the file has 1600 lines without reading it into R first...
system only returns the return value of your command by default, you need to use its intern argument:
system(paste("wc -l banner.p"), intern=T)->kt
kt would then be some string like
<lines> <filename>
And then you could parse the string.
I have the following system variable in .zshrc
manuals='/usr/share/man/man<1-9>'
I run unsuccessfully
zgrep -c compinit $manuals/zsh*
I get
zsh: no matches found: /usr/share/man/man<1-9>/zsh*
The command should be the same as the following command which works
zgrep -c compinit /usr/share/man/man<1-9>/zsh*
How can you run the above command with a system variable in Zsh?
Try:
$> manuals=/usr/share/man/man<0-9>
$> zgrep -c compinit ${~manuals}/zsh*
The '~' tells zsh to perform expansion of the <0-9> when using the variable. The zsh reference card tells you how to do this and more.
From my investigations, it looks like zsh performs <> substitution before $ substitution. That means when you use the $ variant, it first tries <> substitution (nothing there) then $ substitution (which works), and you're left with the string containing the <> characters.
When you don't use $manuals, it first tries <> substitution and it works. It's a matter of order. The final version below shows how to defer expansion so they happen at the same time:
These can be seen here:
> manuals='/usr/share/man/man<1-9>'
> echo $manuals
/usr/share/man/man<1-9>
> echo /usr/share/man/man<1-9>
/usr/share/man/man1 /usr/share/man/man2 /usr/share/man/man3
/usr/share/man/man4 /usr/share/man/man5 /usr/share/man/man6
/usr/share/man/man7 /usr/share/man/man8
> echo $~manuals
/usr/share/man/man1 /usr/share/man/man2 /usr/share/man/man3
/usr/share/man/man4 /usr/share/man/man5 /usr/share/man/man6
/usr/share/man/man7 /usr/share/man/man8
The following does work as expected:
date +'%d-%b-%Y-%H-%M'
28-Sep-2009-14-28
But none of the following 4 entries from crontab are working.
* * * * * date +\'%d-%b-%Y-%H-%M\' >> /backup/shantanu/testing.txt
* * * * * date +'%d-%b-%Y-%H-%M' >> /backup/shantanu/testing1.txt
* * * * * date +"%d-%b-%Y-%H-%M" >> /backup/shantanu/testing2.txt
* * * * * date +\"%d-%b-%Y-%H-%M\" >> /backup/shantanu/testing3.txt
Error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
I can save the same code in a shell script and set the cron, but I will like to know if it is possible to directly set a cron for the task.
The actual cron entry that I am trying to set looks something like this...
16 * * * * mysqldump myDB myTB > /backup/ABCbc$(date +'%d-%b-%Y-%H-%M').sql 2> /backup/ABCbc_errORS$(date +'%d-%b-%Y-%H-%M').txt
There are four common causes for cron job commands to behave differently compared to commands typed directly into an interactive shell:
Cron provides a limited environment, e.g., a minimal $PATH, and other expected variables missing.
Cron invokes /bin/sh by default, whereas you may be using some other shell interactively.
Cron treats the % character specially (it is turned into a newline in the command).
The command may behave differently because it doesn't have a terminal available.
You must precede all % characters with a \ in a crontab file, which tells cron to just put a % in the command, e.g.
16 * * * * mysqldump myDB myTB > "/backup/ABCbc$(date +'\%d-\%b-\%Y-\%H-\%M').sql" 2> "/backup/ABCbc_errORS$(date +'\%d-\%b-\%Y-\%H-\%M').txt"
(As a separate matter, always put double quotes around a "$variable_substitution" or a "$(command substitution)", unless you know why not do it in a particular case. Otherwise, if the variable contents or command output contains whitespace or ?*\[, they will be interpreted by the shell.)
As long as there are no spaces in the format string supplied as an argument to date, you should not need the ticks at all.
date +%d-%b-%Y-%H-%M
should work.
You're using a syntax not supported by /bin/sh. Try invoking your preferred shell and passing the command as an argument.