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)
Related
I have written a QT GUI program where pressing a button will execute a .sh script. The contents of the script is-
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
basically the script will import a .csv to an sqlite database. And when the script file (script.sh) is run manually from linux terminal ($./script.sh) it successfully imports the .csv file into the database table.
But, when I call the script from my QT program
void MainWindow::on_importButton_clicked()
{
QProcess process;
process.startDetached("/bin/sh",QStringList()<<"/home/aj/script.sh");
}
it compiles successfully but gives an error message in console when the button is pressed at runtime.
Error: near line 1: near "-": syntax error
Error: cannot open "ora_exported.csv"
what could be causing this ???
EDITED
I have changed my .sh script now to--
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
Thus providing the path to my ora_exported.csv. As a result the runtime error [Error: cannot open "ora_exported.csv"] has gone but the other message [Error: near line 1: near "-": syntax error] is still coming.
Same as was observed in previous case, using ./script.sh is successfully importing data to sqlite3 db table file but QProcess is unable to.
echo is a built in command of a shell that may behave differently.
E.g. take this test script: echotest.sh
echo -e "123"
Now we can compare different results:
$ bash echotest.sh
123
$ zsh echotest.sh
123
$ dash echotest.sh
-e 123
You are probably on some Ubuntu-like OS, where /bin/sh redirects to dash. That would explain the error around "-". So if you are using echo, set you shell specificially or ensure that your script works on all common shells.
Additionally, you are messing up your quotations
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported'
results in (no quotations in the first line)
attach database testdatabase.db as aj;
.separator ","
.import /home/aj/ora_exported.csv qt_ora_exported
but you pobably want
echo -e "attach database 'testdatabase.db' as 'aj';\n.separator ','\n.import /home/aj/ora_exported.csv qt_ora_exported"
It looks strange that you are using external script to update database!
why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{
QProcess::startDetached("/bin/sh",
QStringList()<<"/home/aj/script.sh",
"<location of: 'ora_exported.csv' file>");
}
I am unable to execute following command in unix as
e.g. ->
export ENVFILE=$PARENT_DIR/../env/.tpms.evr
while i try to execute . "${ENVFILE}" it shows me error as bash: 39910-: command not found
can anybody let me know about how to fix this.
You most probably have a line in .tpms.evr script which bash tries to execute as a command called "39910-"
Please share this .tpms.evr script or just that line containing "39910-"
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.
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=""))
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.