I am running a simple client-server program written in python, on my android phone using QPython and QPython3. I need to pass some commandline parameters. How do I do that?
I found a couple of way of running a script that I imported from my Linux laptop.
If I put frets.py in the script3 directory, and create this script in the same directory:
import sys, os
dir = '/storage/emulated/0/com.hipipal.qpyplus/scripts3/'
os.chdir(dir)
def callfrets(val):
os.system(sys.executable+" frets.py " + val)
while True:
val = input('$:')
if val:
callfrets(val)
else:
break
I can run the program with the same commandline inputs that I used in Linux, getting output on the console. Just invoke this script from the editor or the programs menu.
I also found (after getting some argparse errors) that I can get to a usable Linux shell by quiting the Python console with sys.exit(1):
import sys
sys.exit(1)
drops me into the shell with the / directory. Changing directory
cd /storage/emulated/0/Download # or to the scripts3 directory
lets me run that original script directly
python frets.py -a ...
This shell has the necessary permisions and $PATH (/data/data/com.hipipal.qpy3/files/bin).
(I had problems getting this working on my phone, but updating Qpython3 took care of that.)
Just write a wrapper script which get the parameters and pass to the real script using some function like execfile, and put the script into /sdcard/com.hipipal.qpyplus/scripts or /sdcard/com.hipipal.qpyplus/scripts3 (for qpython3).
Then you can see the script in scripts when clicking the start button.
Related
I have this c based program to do some benchmarking and whatnot which I use heavily. I am a ROS noob, just starting to delve into ROS code to do some analysis.
Currently, I need to be able to call ros based commands (i.e. rosbag play, roscore, etc) through the system() function.
For those who do not know how it works, it basically just executes the command (i.e. system("ls"); system("./testScript.sh").
Right now I have a bash script set up to essentially run several small things including ROS commands. Executing the script works perfectly, but executing it through system always give me an error regarding ROS_cmd: Not found.
For example:
#/bin/bash
ls
echo hi
rosbag play rotating_detergent_1_6.bag
Gives me output error of : rosbag: not found - all the while executing the ls and echo
This happens independent of the ROS based command.
Any one know what is wrong?
edit: So to add, the C program is called in sudo. I noticed that the problem seems to be that system() executing in sudo does not have the appropriate ROS PATH.
For example:
echo $PATH -> /opt/ros/melodic/bin : rest
sudo su; echo $PATH -> rest
How should I go about properly addressing this?
ROS environment is a bunch of variables:
ROS_DISTRO
ROS_ETC_DIR
ROS_PACKAGE_PATH
ROS_PYTHON_VERSION
ROS_VERSION
ROS_ROOT
ROS_MASTER_URI
ROSLISP_PACKAGE_DIRECTORIES
Unless you want to set them all by yourself, you can use a very convenient uitlity script provided by ROS. The script is usually at /opt/ros/<ros_distro>/setup.bash
Just source the script in the terminal where you run your C program and then run your program.
source /opt/ros/<ros_distro>/setup.bash
You should not need sudo but in case you do , use it with -E flag to preserve the environment.
SHORT: How do I explicitly set the current working directory?
LONG: So I have 52 programs daisy chained together. I have a shell script pipeline that works great. Only problem is I can only run it if I cd into the directory with the files and run it. Some of the sub-programs do not have a mechanism that allows me to explicitly set output directories. They dump everything into the current working directory. This is fine if you are running 1 instance of this pipeline, but not so great if you are trying to process a dozen data-sets one after another. I know I can get the current working directory with:
echo $PWD
But how do I set it?
You can set the current dirctory for individual programs in your pipeline without affecting the other program in your pipeline like this:
PWD=path1 command1 && PWD=path2 command2
In general, you can set any environment variable using that syntax. Here is a real example I tried in bash:
$ PWD=/home ./test.rb && PWD=/ ./test.rb
Running in /home
Running in /
In Operating System concept i want to write a C program to make Unix command works as DOS commands.
It Means when ever i press Unix Command like ls -which is used for Display list of Files- it works like Dir command in DOS.Could you please help me out with this?
Starting with the ls command as example, take input from user for the command. If the command is ls call a windows function that will display the content of the current working directory(). For ls,
you need to get first the current working directory. GetCurrentDir() for windows will be your first step. This will help How do I get the directory that a program is running from?
Then you can list the files in directory like this https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx. But you do need the output of first step.
Other commands can also be implemented like this
I have a bunch of shell scripts that used to run on a Linux machine. Now, we've switched over to Windows, and I need to run these scripts there. I have Cygwin installed, but is there a way to make the script run using Cygwin, but the call is made from Windows batch?
Sure. On my (pretty vanilla) Cygwin setup, bash is in c:\cygwin\bin so I can run a bash script (say testit.sh) from a Windows batch file using a command like:
C:\cygwin\bin\bash testit.sh
... which can be included in a .bat file as easily as it can be typed at the command line, and with the same effect.
One more thing - if You edited the shell script in some Windows text editor, which produces the \r\n line-endings, cygwin's bash wouldn't accept those \r. Just run dos2unix testit.sh before executing the script:
C:\cygwin\bin\dos2unix testit.sh
C:\cygwin\bin\bash testit.sh
If you have access to the Notepad++ editor on Windows there is a feature that allows you to easily get around this problem:
Open the file that's giving the error in Notepad++.
Go under the "Edit" Menu and choose "EOL Conversion"
There is an option there for "UNIX/OSX Format." Choose that option.
Re-save the file.
I did this and it solved my problems.
Hope this helps!
Read more at http://danieladeniji.wordpress.com/2013/03/07/microsoft-windows-cygwin-error-r-command-not-found/
Just wanted to add that you can do this to apply dos2unix fix for all files under a directory, as it saved me heaps of time when we had to 'fix' a bunch of our scripts.
find . -type f -exec dos2unix.exe {} \;
I'd do it as a comment to Roman's answer, but I don't have access to commenting yet.
The existing answers all seem to run this script in a DOS console window.
This may be acceptable, but for example means that colour codes (changing text colour) don't work but instead get printed out as they are:
there is no item "[032mGroovy[0m"
I found this solution some time ago, so I'm not sure whether mintty.exe is a standard Cygwin utility or whether you have to run the setup program to get it, but I run like this:
D:\apps\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico bash.exe .\myShellScript.sh
... this causes the script to run in a Cygwin BASH console instead of a Windows DOS console.
If you don't mind always including .sh on the script file name, then you can keep the same script for Cygwin and Unix (Macbook).
To illustrate:
1. Always include .sh to your script file name, e.g., test1.sh
2. test1.sh looks like the following as an example:
#!/bin/bash
echo '$0 = ' $0
echo '$1 = ' $1
filepath=$1
3. On Windows with Cygwin, you type "test1.sh" to run
4. On a Unix, you also type "test1.sh" to run
Note: On Windows, you need to use the file explorer to do following once:
1. Open the file explorer
2. Right-click on a file with .sh extension, like test1.sh
3. Open with... -> Select sh.exe
After this, your Windows 10 remembers to execute all .sh files with sh.exe.
Note: Using this method, you do not need to prepend your script file name with bash to run
I need a shell script which will allow me to typeset Lilypond files from TextWrangler (A Mac App).
So far I have come up with this:
#!/bin/sh
/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1
which, of course, doesn't work. (That's why I'm at Stack Overflow.)
When I run that script from the shebang menu in TextWrangler, I get this output:
/Applications/LilyPond.app/Contents/Resources/bin/lilypond: option faultpaper,
--output'' requires an argument
What gives?
I'm running Snow Leopard, TextWrangler, and Lilypond.
Help appreciated.
EDIT: Found a way to get the document path in a Unix Script launched by TextWrangler, so I've rewritten this.
There are multiple ways to work with scripts in TextWrangler through the #! menu, and I'm not sure which one you're trying to use. It looks, though, like you're trying to create a Unix Script to convert your LilyPond document.
As your error hints, Unix Scripts unfortunately aren't given any arguments at all, so $1 will be empty. However, it turns out that recent versions of BBEdit/TextWrangler do set some environment variables before running your script (see BBEdit 9.3 Release Notes and scroll down to Changes). In particular, you can use the following environment variable:
BB_DOC_PATH path of the document (not set if doc is unsaved)
So, save this script to ~/Library/Application Support/TextWrangler/Unix Support/Unix Scripts and you should be good to go.
Other ways you might be trying to do this that don't work well:
Using a Unix Filter: to do this you would have to select all of your LilyPond code in the document, and it would be saved into a temporary file, which is passed as an argument to your script. OK, so that gets you an input filename, at the cost of some hassle. But then the output of that script (i.e. the LiiyPond compiler output) by default replaces whatever you just selected, which is probably not what you want. Wrong tool for the job.
Using #! → Run on a LilyPond file: This involves putting a #! line at the top of your file and having TextWrangler attempt to execute your file as a script, using the #! as a guide to selecting the script interpreter. Unfortunately, the #! line only works with certain scripting languages, and LilyPond (not quite a scripting language) isn't one of them. This is what Peter Hilton is trying to do, and as he notes, you will get LilyPond syntax errors if you try to add a #! line to the top of a LilyPond file. (If you're curious, there is technically a way to get #! → Run to work, which is to embed your LilyPond code inside an executable shell or perl script, using here-document syntax. But this is a gross hack that will quickly become unwieldly.)
There are a few limitations to the script linked above:
It doesn't check to see whether you saved your document before running LilyPond. It would be nice to have TextWrangler automatically save before running LilyPond.
It can't take snippets of text or unsaved documents as input, only saved documents.
You can make more sophisticated solutions that would address these by turning to AppleScript. Two ways of doing this:
Create a script that's specific to TextWrangler and drop it in ~/Library/Application Support/TextWrangler/Scripts. It then shows up in the AppleScript menu (the weird scrolly S), or you can get at it by bringing up Window → Palettes → Scripts. I believe two folks out there have gone down this path and shared their results:
Henk van Voorthuijsen (Lilypond.applescript extracted from MacOS 10.5 Applescript for TextWrangler thread on lilypond-devel, 21-Jul-2008)
Dr Nicola Vitacolonna (LilyPond in TextWrangler – uses TeXShop).
Create a Mac OS Service, which would potentially be a method that would be reusable across just about any text editor. This was how we used to compile Common Music files way back in the NeXT days, so I can testify to its elegance. I don't have a good up-to-date example of this, unfortunately.
Good question. It actually runs Lilypond on my system if you do this:
#!/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1
… but fails because # is not a line-comment character so Lilypond tries to parse the line.
Surrounding it with a block comment fails because TextWrangler cannot find the ‘shebang’ line.
%{
#!/Applications/LilyPond.app/Contents/Resources/bin/lilypond -o $1
%}
An alternative is to use Smultron 3, which lets you define commands that you can run with a keyboard shortcut.