I am trying to create a file using the system command but it does not work for some reason. It just echoes the command back.
system("echo 'Hello2222, world.' >foo2.txt");
Hello2222, world. >foo2.txt
When I run the echo command in CMD, the file is created.
Notice that the documentation for system says
On Windows, system does not use a shell and there is a separate
function shell which passes command lines to a shell.
shell("echo 'Hello2222, world.' >foo2.txt")
will do what you want.
Related
I am trying to learn shell on Linux,
but I've got a problem which seems confusing.
My environment is:
OS: Manjaro 21.2.6 Qonos
Kernel: x86_64 Linux 5.15.38-1-MANJARO
Shell: zsh 5.8.1 (x86_64-pc-linux-gnu)
The problem is:
I created a file named foo, and echoed #\!/bin/sh to it, and the permission of file foo has been modified to 100 by using chmod.
The file foo doesn't have the read or write permission indeed, that's for true,
but when I executed the command ./foo, I got the error /bin/sh: ./foo: permission denied.
So why the Shell knows what the shebang in the file foo is without the read permission ???
If anyone of you can proide any suggesstions, I will be really thankful !
behavior-example
So why the Shell knows what the shebang in the file foo is without the read permission ???
It is not the shell that reads the shebang line but the OS/kernel.
A shell script can be executed in the same way as a compiled program. The process uses a function of the exec* family and passes ./foo as the program to execute. These functions are based on system calls.
The OS/kernel then detects if the file is a compiled program which can be executed directly or a script file which must be passed to an interpreter. If the file contains a shebang line, the OS will execute the specified interpreter, which does not have to be a shell, otherwise it will run the default shell. The script file is passed as an argument to the interpreter.
The shell is running with normal user permissions and will get an error when it tries to open the script file.
You can find some information about executiong scripts in the POSIX specification of the exec function family or in the Linux manual page for execve. Search for the word interpreter. You could check the Linux kernel source code for more details.
You get this error because the script itself do not have execution or read permission. To run script in shell (on your way) you need to have read and execute permissions:
chmod 500 foo
./foo
the other way to run the script is:
sh foo
and you do not need execution permission in this case. For the record the standard way is to use construction like:
#!/bin/sh
You do not need to escape exclamation mark
shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
This is the call to script.On calling the above script, the error occurs.
I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.
Add the Rscript path to your environment variables in Windows:
Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"
Restart everything. Should be good to go. Then you should be able to do
exec('Rscript PATH/TO/my_code.R')
instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.
You need to set the proper path where your RScript.exe program is located.
exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
C:\\My_work\\R_scripts\\my_code.R my_args";
#my_args only needed if you script take `args`as input to run
other way is you declare header in your r script (my_code.r)
#!/usr/bin/Rscript
and call it from command line
./my_code.r
If you are running it in Git Bash terminal, you could follow a revised version of the idea suggested by #user5249203: in the first line of your file my_code.R, type the following
#!/c/R/R-3.2.2/bin/Rscript.exe
I assumed that your path to Rscript.exe is the one listed above C:\R\R-3.2.2\bin. For anyone having a different path to Rscript.exe in Windows, just modify the path-to-Rscript accordingly. After this modification of your R code, you could run it in the Git Bash terminal using path-to-the-code/mycode.R. I have tested it on my pc.
I faced the same problem while using r the first time in VS Code, just after installing the language package (CRAN).
I restart the application and everything worked perfectly. I think restarting would work for you as well.
I have some command line batch code which I can run in my windows command prompt just fine. I'm basically pushing a local file text file to a remote server using WinSCP command line arguments - https://winscp.net/eng/docs/commandline. These are the commands I use, in order:
to open up winscp command line:
winscp
then to open connection to my server through ssh:
open sftp://myUserName:myPassword#theRemoteServer.net
upload file to remote server:
put directoryMyLocalFileIsIn\fileToUpload.csv /locationOnRemoteServer/whatToNameFileOnRemoteServer.csv
then close connection:
close
This all works fine. But, I want to run this all from within RStudio.. My issue -
after I run:
shell.exec("winscp")
I can see the winscp shell is opened up. But when I try and run my next commands like these:
shell.exec("open sftp://myUserName:myPassword#theRemoteServer.net")
It just doesnt run in that winscp shell that opened up.. I've also used all sorts of combinations of R commands like shell, system2 and shell.
Again, I can open up the winscp shell successfully from within R. But I cant figure out how to then run commands in that shell. Anyone know how to do this?
Thank you.
You need to call WinSCP and specify all commands using a single call from R. The best way to do this is to save your WinSCP commands in a single text file, e.g. myscript.txt:
open sftp://myUserName:myPassword#theRemoteServer.net
put directoryMyLocalFileIsIn\fileToUpload.csv /locationOnRemoteServer/whatToNameFileOnRemoteServer
exit
Then, from the command line, you can call WinSCP as follows (see the WinSCP documentation):
winscp.com /script=myscript.txt
(you might need to specify the exact path for WinSCP and myscript.txt)
Finally, to accomplish this from R, use the system2 command as follows:
system2(
"winscp.com",
args = c("/script=myscript.txt"))
Again, you might need to specify the paths to winscp.com and myscript.txt.
I am executing a ksh script named abs.ksh located at /app/fao.... which connects to a server,
But the server is receiving a script named "ksh" which is present in /usr/bin...
I am not calling any script called ksh in abs.ksh(sorry cannot paste the code).
Also this happens only when the script is run in debug mode.
In non debug mode it works fine.
Can anyone give me a hint of what might be happening here.
In a standard "classic" Unix environment there may be multiple shells. E.g. 'sh' the original Bourne shell, 'ksh' - the Korn shell, csh - the C shell, bash, tcsh etc. etc.. A user login will have the default shell set per login.
The #! at the start of an executable script is an instruction to interpret & run the subsequent text with the name of the program following the '#!'.
E.g. run this with perl
#!/bin/perl
<.. perl stuff ..>
So yes #!/usr/bin/ksh - will run the script with the command interpreter (shell) at that location.
Need more info. wrt how you run in debug mode. I.e. are you typing 'ksh -x ...' or 'sh -x' - if so where is that on your search path. E.g. 'whence ksh' - maybe you're running with a different shell in debug mode.
Also which os is this ?
I'm trying to call a script in Tcl with the command:
exec source <script path>
and I get the error
couldn't execute "source": no such file or directory
How can I call another script from tcl?
Edit: I am running a command I got from another person in my office. I was instructed to run "source " explicitly with source. So in other words, how would I run any command that would work in cshell, in Tcl?
If the script you were given is a cshell script, you can exec it like this:
exec /bin/csh $path_to_script
In effect, this is what the 'source' command does from within an interactive shell. It's not clear whether this is really what you want to do or not (not exactly, but close enough for this discussion).
The reason you can't exec the source command is that exec will only work on executable files (hence the name 'exec'). The source command isn't implemented as an exectuable file, it is a command built-in to the shell. Thus, it can't be exec'd.
If you really feel the need to exec the source command or any other built-in command you can do something like this:
exec /bin/csh -c "source $path_to_script"
In the above example you are execing the c shell, and asking it to run the command "source ". For the specific case of the source command, this doesn't really make much sense.
However, I'm not sure any of this will really do what you expect. Usually if someone says "here's some commands, just do 'source ', it usually just defines some aliases and whatnot to be used from within an interactive shell. Those aliases won't work from within Tcl.
source in csh, like . in bash, executes a script without spawning a new process.
The effect is that any variable that is set in that script is available in current csh session.
Actually, source is a built-in command of csh, thus not available from tcl exec, and using exec without source would not give the specific source effect.
There is no simple way to solve your problem.
source load the source file
you should do:
source <script path>
If you want to execute it, then you need to call the main proc.
another option would be to do:
exec [info nameofexecutable] <scritp path>
Some confusion here. exec runs a separate program, possibly with arguments.
source is not a separate program, it is another Tcl command which reads a file of Tcl commands and executes them, but does not pass arguments. If the other script you are trying to call is written to be run on from the command line, it will expect to find its arguments as a list in variable argv. You can fake this by setting argv to the list of arguments before running source, eg.
set argv {first_arg second_arg}
source script_path
Alternatively you could use exec to start a whole separate Tcl executable and pass it the script and arguments:
exec script_path first_arg second_arg
the error speaks for itself. Make sure you give the correct path name, specify full path if necessary. and make sure there is indeed the file exists in that directory
Recently I wanted to set some UNIX environment variables by sourcing a shell script and stumbled across the same problem. I found this simple solution that works perfectly for me:
Just use a little 3-line wrapper script that executes the source command in a UNIX shell before your Tcl script is started.
Example:
#!/bin/csh
source SetMyEnvironment.csh
tclsh MyScript.tcl