I am building a minimal standard bash menu. The selections works fine but commands are not being executed. I suspect I might to wrap in the command, but not sure how that is being done. I will often have 2 commands sent in, thus the commands needs to be separated in a way that bash understand it as line of commands.
I found this SO question with a similar title, but not answering why commands inside the bash script are not being executed:
Cannot execute shell commands in bash script
What works and not?
Press 1: Does not change to correct cd.
Press 2: Creates the file in correct folder.
Press 3: Works.
Press 4: Works (R file is prepared with: a <- 1).
Wanted behaviour:
I need the commands inside the bash menu script, to be executed.
#!/bin/bash
# -----
# Menu:
# -----
while :
do
echo "Menu"
echo "1 - Change directory to /tmp"
echo "2 - Create file test1.sh with hello message, in /tmp"
echo "3 - Execute test1.sh"
echo "4 - Execute R-script [/tmp/test2.R]"
echo "Exit - any kind but not [1-4]"
read answer;
case $answer in
1)
echo "Change directory to [\tmp]"
cd /tmp # Command to be executed.
break
;;
2)
echo "Create file [test1.sh] in [\tmp]"
# Commands to be executed.
cd /tmp
touch test1.sh
chmod +x test1.sh
echo echo "hello" > test1.sh
break
;;
3)
echo "Execute file [test1.sh]"
/tmp/./test1.sh # Command to be executed.
break
;;
4)
echo "Execute R-script [/tmp/test2.R]"
cd /tmp && /usr/bin/Rscript test2.R # Command to be executed.
break
;;
*)
# Command goes here
echo "Exit"
break
;;
esac
done
you should not use break when you want to execute all cases.
and also in case 3 you used /tmp/./test1.sh it should be ./tmp/test1.sh or sh /tmp/test1.sh
your code should be:
#!/bin/bash
# -----
# Menu:
# -----
while :
do
echo "Menu"
echo "1 - Change directory to /tmp"
echo "2 - Create file test1.sh in /tmp"
echo "3 - Execute test1.sh"
echo "4 - Execute R-script [/tmp/test2.R]"
echo "Exit - any kind but not [1-4]"
read answer;
case $answer in
1)
echo "Change directory to [\tmp]"
cd /tmp # Command to be executed.
pwd
;;
2)
echo "Create file [test1.sh] in [\tmp]"
touch /tmp/test1.sh # Command to be executed.
;;
3)
echo "Execute file [test1.sh]"
./tmp/test1.sh # Command to be executed.
;;
4)
echo "Execute R-script [/tmp/test2.R]"
/usr/bin/Rscript /production/20_front_trader/build/x_run_front_trader.R # Command to be executed.
;;
*)
# Command goes here
echo "Exit"
;;
esac
done
Related
I am trying to source a third party script in zsh (named setup_env.sh stored in ~/), that has following lines in the beginning to guard against accidental execution:
#!/bin/sh
# Guard the script against execution - it must be sourced!
echo $0 | egrep 'setup_env.sh' > /dev/null
if [ $? -eq 0 ]; then
echo ""
echo "ERROR: the setup file must be SOURCED, NOT EXECUTED in a shell."
echo "Try (for bash) : source setup_env.sh"
echo "Or (eg. for ksh): . setup_env.sh"
exit 1
fi
# export some environment variables
...
When I source this script with source ~/setup_env.sh, I see the error message shown in the above code block.
From the script it's apparently visible that it's not written with zsh in mind. But I still want to know why zsh behaves this way, and if it's possible to source the script as it is.
I could source the script as it is without error using bash.
I could also source it in zsh after commenting out the guard block in the beginning of the script.
Can someone explain this difference in behavior for source command between zsh and bash?
zsh/bash have different ways to detect sourcing, following should work for both :
if [[ -n $ZSH_VERSION && $ZSH_EVAL_CONTEXT == toplevel ]] || \
[[ -n $BASH_VERSION && $BASH_SOURCE == $0 ]]; then
echo "Not sourced"
exit 1
fi
To explain a little more, when you run :
source setup_env.sh
# setup_env.sh containing "echo $0"
In zsh, $0 == setup_env.sh
In bash, $0 == bash
I have a a Shell script that contain a Perl script and R script.
my Shell script R.sh:-
#!/bin/bash
./R.pl #calling Perl script
`perl -lane 'print $F[0]' /media/data/abc.cnv > /media/data/abc1.txt`;
#Shell script
Rscript R.r #calling R script
This is my R.pl (head):-
`export path=$PATH:/media/exe_folder/bin`;
print "Enter the path to your input file:";
$base_dir ="/media/exe_folder";
chomp($CEL_dir = <STDIN>);
opendir (DIR, "$CEL_dir") or die "Couldn't open directory $CEL_dir";
$cel_files = "$CEL_dir"."/cel_files.txt";
open(CEL,">$cel_files")|| die "cannot open $file to write";
print CEL "cel_files\n";
for ( grep { /^[\w\d]/ } readdir DIR ){
print CEL "$CEL_dir"."/$_\n";
}close (CEL);
The output of Perl script is input for Shell script and Shell's output is input for R script.
I want to run the Shell script by providing the input file name and output file name like :-
./R.sh home/folder/inputfile.txt home/folder2/output.txt
If folder contain many files then it will take only user define file and process it.
Is There is a way to do this?
I guess this is what you want:
#!/bin/bash
# command line parameters
_input_file=$1
_output_file=$2
# #TODO: not sure if this path is the one you intended...
_script_path=$(dirname $0)
# sanity checks
if [[ -z "${_input_file}" ]] ||
[[ -z "${_output_file}" ]]; then
echo 1>&2 "usage: $0 <input file> <output file>"
exit 1
fi
if [[ ! -r "${_input_file}" ]]; then
echo 1>&2 "ERROR: can't find input file '${input_file}'!"
exit 1
fi
# process input file
# 1. with Perl script (writes to STDOUT)
# 2. post-process with Perl filter
# 3. run R script (reads from STDIN, writes to STDOUT)
perl ${_script_path}/R.pl <"${_input_file}" | \
perl -lane 'print $F[0]' | \
Rscript ${_script_path}/R.r >"${_output_file}"
exit 0
Please see the notes how the called scripts should behave.
NOTE: I don't quite understand why you need to post-process the output of the Perl script with Perl filter. Why not integrate it directly into the Perl script itself?
BONUS CODE: this is how you would write the main loop in R.pl to act as proper filter, i.e. reading lines from STDIN and writing the result to STDOUT. You can use the same approach also in other languages, e.g. R.
#!/usr/bin/perl
use strict;
use warnings;
# read lines from STDIN
while (<STDIN>) {
chomp;
# add your processing code here that does something with $_, i.e. the line
# EXAMPLE: upper case first letter in all words on the line
s/\b([[:lower:]])/\U\1/;
# write result to STDOUT
print "$_\n";
}
What is a neat command line equivalent to RStudio's Knit HTML? Given an .Rmd file, you can use RStudio to knit .html, .docx and .pdf files using Knitr. It would be great to shift this process completely to the command line. My approach so far:
Rscript -e "library(knitr); knit('test.Rmd')" # This creates test.md
pandoc test.md >> test.html
This works fine, but the resulting test.html does not come with the same pretty make over as in RStudio. Any suggestions how one should best knit .Rmd files to .html via the command line, and end up with a pretty .html?
Extra question: What would be the best command line solution for .pdf or .docx?
rmarkdown::render("test.Rmd", "html_document")
Following up on the accepted answer, I've drafted a bash script called "knitter" that will do everything needed, all the user needs to do is input: ./knitter file.Rmd file.html or ./knitter file.Rmd file.pdf.
The script is below:
#!/bin/sh
### Test usage; if incorrect, output correct usage and exit
if [ "$#" -gt 2 -o "$#" -lt 2 ]; then
echo "********************************************************************"
echo "* Knitter version 1.0 *"
echo "********************************************************************"
echo -e "The 'knitter' script converts Rmd files into HTML or PDFs. \n"
echo -e "usage: knitter file.Rmd file.{pdf,html} \n"
echo -e "Spaces in the filename or directory name may cause failure. \n"
exit
fi
# Stem and extension of file
extension1=`echo $1 | cut -f2 -d.`
extension2=`echo $2 | cut -f2 -d.`
### Test if file exist
if [[ ! -r $1 ]]; then
echo -e "\n File does not exist, or option mispecified \n"
exit
fi
### Test file extension
if [[ $extension1 != Rmd ]]; then
echo -e "\n Invalid input file, must be a Rmd-file \n"
exit
fi
# Create temporary script
# Use user-defined 'TMPDIR' if possible; else, use /tmp
if [[ -n $TMPDIR ]]; then
pathy=$TMPDIR
else
pathy=/tmp
fi
# Tempfile for the script
tempscript=`mktemp $pathy/tempscript.XXXXXX` || exit 1
if [[ $extension2 == "pdf" ]]; then
echo "library(rmarkdown); rmarkdown::render('"${1}"', 'pdf_document')" >> $tempscript
Rscript $tempscript
fi
if [[ $extension2 == "html" ]]; then
echo "library(rmarkdown); rmarkdown::render('"${1}"', 'html_document')" >> $tempscript
Rscript $tempscript
fi
My simpler command-line script, similar to Tyler R.'s:
In your .profile or similar, add:
function knit() {
R -e "rmarkdown::render('$1')"
}
Then, on command line, type knit file.Rmd
EDIT: For nice autocomplete, see comments
I set up output format in the Rmd header: output: github_document or similar
getwd()
setwd("C:Location of the RMD File")
# To make it in to PDF you can use the below code also
rmarkdown::render("Filname.Rmd")
# To make it in to PDF you can use the below code also
rmarkdown::render("Filename", "pdf_document")
I Typed the above in to a R Script and triggered it from Python command prompt and solved my requirement:)
Kindly note : if it doesn't work.. Try to install latex and try again.. All the best :)
From the mac/linux terminal, you could run:
R -e "rmarkdown::render('README.Rmd')"
(replacing README.Rmd with whatever file you wish to knit)
For my simulations, I wrote a little loop to run multiple (n=20) instances of "my_script.R" simultaneously:
#! /bin/bash
declare -i i=1 n=20
while [ $i -le $n ]; do
echo "#!/bin/bash --login" >my.qsub.${i}
echo "#PBS -l nodes=1:ppn=1" >> mu.qsub.${i}
echo "#PBS -l mem=2GB" >> my.qsub.${i}
echo "cd /~path to my wd/" >> my.qsub.${i}
echo "module load R/3.0.1" >> my.qsub.${i}
echo -n 'R CMD BATCH --vanilla --slave --no-timing my_script.R ' >> my.qsub.${i}
echo "" >> my.qsub.${i}
qsub -l walltime=03:59:00 my.qsub.${i}
sleep 2
let i+=1
done
Within "my_script.R", I have a for loop (n=1,...,B), which writes it's results into a "fnam" .txt file in the end of each iteration:
cat(file=fnam, append=TRUE, Results[n,], "\n")
Everything works fine, BUT it looks like there may be a problem if two different instances of the same R scrip try to append the fnam file simultaneously.
Did anyone try to synchronize/order the way, in which multiple instances of the same R script append the same output file?
They jobs will need to write to different files which you will have to combine after. You can submit all the jobs at once as as 'Array Job' using qsub -t 20 -sync y. That will create 20 identical jobs and wait for all of them to finish before returning. Each job can get a unique identifier for itself via the environment variable SGE_TASK_ID from which it can craft a unique filename. The sync option will make qsub wait until they complete before returning and then you can concatenate all the files together.
The unix tee command splits the standard input to stdout AND a file.
What I need is something that works the other way around, merging several inputs to one output - I need to concatenate the stdout of two (or more) commands.
Not sure what the semantics of this app should be - let's suppose each argument is a complete command.
Example:
> eet "echo 1" "echo 2" > file.txt
should generate a file that has contents
1
2
I tried
> echo 1 && echo 2 > zz.txt
It doesn't work.
Side note: I know I could just append the outputs of each command to the file, but I want to do this in one go (actually, I want to pipe the merged outputs to another program).
Also, I could roll my own, but I'm lazy whenever I can afford it :-)
Oh yeah, and it would be nice if it worked in Windows (although I guess any bash/linux-flavored solution works, via UnxUtils/msys/etc)
Try
( echo 1; echo 2 ) > file.txt
That spawn a subshell and executes the commands there
{ echo 1; echo 2; } > file.txt
is possible, too. That does not spawn a subshell (the semicolon after the last command is important)
I guess what you want is to run both commands in parallel, and pipe both outputs merged to another command.
I would do:
( echo 1 & echo 2 ) | cat
Where "echo 1" and "echo 2" are the commands generating the outputs and "cat" is the command that will receive the merged output.
echo 1 > zz.txt && echo 2 >> zz.txt
That should work. All you're really doing is running two commands after each other, where the first redirects to a file, and then, if that was successful, you run another command that appends its output to the end of the file you wrote in the first place.