How to call the function - unix

I am trying to invoke a function from command prompt, but I am not finding the correct way to invoke it.
This is my script:
echo "Hello World!"
test {
echo "Sample message"
}
I tried below ways:
sh-4.2$ main.sh test
Hello World!
./main.sh: line 5: usuage: command not found
A helpful message!
./main.sh: line 8: syntax error near unexpected token `}'
./main.sh: line 8: `}'
sh-4.2$ . main.sh test
Hello World!
sh: usuage: command not found
A helpful message!
sh: ./main.sh: line 8: syntax error near unexpected token `}'
sh: ./main.sh: line 8: `}'
sh-4.2$ . main.sh test()
sh: syntax error near unexpected token `('
Can you please help me in this.

Couple of issues here, the syntax for test function is wrong, you need have the parentheses around,
test() {
echo "Sample message"
}
export -f test
The export -f syntax allows you to export your functions to the shell; to run them from the command-line you need to source the script in the current shell as,
$ . ./main.sh
Hello World!
Now you can call the function test directly from the command line after having exported it from the script,
$ test
Sample message
Also a good practice to NOT have functions name test, because it is same name a shell built-in. Recommend using some custom names for that.

Related

Rendering many Rmds from command line using GNU parallel

To knit an Rmd from the command line, you can do the following and it creates an HTML
Rscript -e "rmarkdown::knit('test.Rmd')"
I want to do this for many Rmds using GNU parallel, I've tried this and various versions of it where I move the quotes around
find -name "*.Rmd" | parallel Rscript -e "rmarkdown::render('{}')"
But I keep getting errors.
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `Rscript -e rmarkdown::render('./test.Rmd')'
I think this is something to do with where the quotation marks are because I get different errors depending on where I put them. What is the problem? Is it doing something funny like only trying to parallelize Rscript and not what comes after it?
From man parallel:
If you get errors like:
sh: -c: line 0: syntax error near unexpected token
sh: Syntax error: Unterminated quoted string
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
zsh:1: no matches found:
then you might try using -q.
So:
find -name "*.Rmd" | parallel -q Rscript -e "rmarkdown::render('{}')"

zsh parse error: "(eval):1: parse error near `)'"

When I open terminal, I receive this message:
Last login: Fri Jan 17 11:02:25 on ttys000
(eval):1: parse error near `)'
(eval):1: parse error near `)'
It prints twice, but I only have four ")" in my .zshrc file, and none of them seem to be causing any issues. I've looked around the web for solutions, but I haven't found many that seem to address this problem.
My .zshrc file looks like this:
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH=$HOME/.oh-my-zsh
(Some code removed for brevity...)
# Which plugins would you like to load?
source $ZSH/oh-my-zsh.sh
# My personal Add-ons:
# open VSCode via "code"
function code {
if [[ $# = 0 ]]
then
open -a "Visual Studio Code"
else
local argPath="$1"
[[ $1 = /* ]] && argPath="$1" || argPath="$PWD/${1#./}"
open -a "Visual Studio Code" "$argPath"
fi
}
Thanks!

Executing R script within R 3.3.1 installed on windows

Sorry for a basic question. I'm trying run a R script called cuffdiff_gtf_attributes (please find it at enter link description here in R 3.3.1 installed on the Windows 7. The script is started with the below line:
#!/usr/bin/env Rscript
When I type cuffdiff_gtf_attributes in R, it says Error: object 'cuffdiff_gtf_attributes' not found. Also, I tried Rscript cuffdiff_gtf_attributes that returned me: Error: unexpected symbol in "Rscript cuffdiff_gtf_attributes".
Moreover, I tried source('cuffdiff_gtf_attributes.R')that seems to work and returned the usage of the script as bellow
Error:
usage: cuffdiff_gtf_attributes --input=<inputGTF> [--output=outputGTF] | --help
But, when I add the arguments as source('cuffdiff_gtf_attributes.R') --input=file.gtf, it says that: Error: object 'file.gtf' not found. I also tried this command as source('cuffdiff_gtf_attributes.R') --input file.gtf, it says that Error: unexpected symbol in "source('cuffdiff_gtf_attributes.R') --input file.gtf"
Sorry, I couldn't post a sample GTF file, you can find a short sample of it at enter link description here
Everything is the current path. Could you please help me out to execute the script?
Thanks in advance
This is a script file. You should run using Rscript instead for Rgui.exe. From a command prompt, navigate to the directory where file.gtf is and run:
"%Programfiles%\R\R-3.3.3\bin\Rscript" cuffdiff_gtf_attributes.R --input=file.gtf

Typeset not found error in ksh

I have written a statement like typeset -l target_tbl. I am getting an error that typeset : not found.
I ran the same statement on prompt. It executed successfully. I am getting this error in script. Can anyone guide me?
Thanks!

Error while running a .sh script via QProcess

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>");
}

Resources