how to run .sh file in Ubuntu using installScript - bash

I am creating an installation using InstallShield 2018 in windows 10. I need to execute a script file (.sh) in Ubuntu from a function in Installscript. I tried the following but it did not work:
szCmdPath = "C:\\Users\\Admin\\AppData\\Local\\Microsoft\\WindowsApps\\Ubuntu.exe";
szCmdLine = ". /mnt/d/test.sh";
LaunchAppAndWait( szCmdPath, szCmdLine, WAIT);
However I can execute the exact same file in Ubuntu Terminal and it works great. I did turn on Window sub system for Linux and install Ubuntu on windows. Why is this happening? Why can I run cmd.exe from installscript but not Ubuntu?
Thank you in advance.
EDIT 1: if I pass an empty string as parameter, Ubuntu is start and waits for my input commands. But when I pass the path to my script file, nothing happened except a flash of the terminal console before my installation goes on running.

From my reading, wsl and ubuntu differ slightly. It looks like wsl is a bit magical and occasionally similar to bash -c or ubuntu -c, whereas you can consider Ubuntu.exe as somewhat equivalent to /bin/bash.
If you try to run /bin/bash . /mnt/d/test.sh from a bash prompt, things don't go well. So the correct approach will depend on the contents of your script and what you need to happen. I think one of the following options are the most likely:
remove . from your command; instead run ...\Ubuntu.exe /mnt/d/test.sh
add -c to your command; instead run ...\Ubuntu.exe -c . /mnt/d/test.sh
Note that %LOCALAPPDATA%\Microsoft\WindowsApps\Ubuntu.exe is a special file (zero bytes), so it's also plausible that it needs some special handling. For instance, maybe it requires a 64-bit caller. If that's the case, you may need to wrap it in a call through a 64-bit cmd prompt. My quick tests don't show this as likely, however, so I think it will work without this extra indirection.

Related

Command prompt isn't integrating anything

I installed amazonCLI, as well as Cygwin, and changed the Path env variable to:
%SystemRoot%\System32\Wbem;C:\Program Files\Prio;C:\Program
Files\Diskeeper Corporation\ExpressCache\;C:\Program
Files\Amazon\AWSCLI\;C:\cygwin64\bin
When I open the command prompt, first of all it is directly pointing to
C:\Users\Stephane
(which I think is weird). And then when I input something like 'ls', the return error is:
'ls' is not recognized as internal or external command, operable program or batch file
Can you please help me know what I am doing wrong?
Thanks
From the error message you got, it's evident, that you are trying to execute Cygwin's commands from the Windows Console. That's also the reason, why it's executed in your Windows' profile directory instead of the Cygwin's one.
You should read the basics about using the Cygwin first, because it seems you don't know what Cygwin exactly is and how to use it. Maybe you don't need the Cygwin at all, it depends on what you need to accomplish. The is for example GnuWin tool set or UnxUtils, which are just a sets of standalone GNU tools compiled for Windows. Cygwin is more like system than standalone utilities. You can for example execute shell scripts under the Cygwin.
So it depends on your needs. But I simply can't imagine using Windows without Cygwin yet.
You can execute Cygwin's shell from the Windows Console, but I advice you to use MinTTY (which is in directory $CYGWIN_DIR/bin). MinTTY is a terminal emulator, which executes your Cygwin shell (bash by default).
To execute for example bash directly from the Windows Console, just execute $CYGWIN_DIR/bash.exe --login -i.

how to run cygwin script in powershell

I am trying to run a cygwin script in powershell. How can I do that?
I tried giving full path of the scripts followed by args . it isnt working
example : /c/script/path/script args
Use either /cygdrive/c/script/path/script or C:/script/path/script (note the capital C and lack of leading slash in the latter path). Plus, need to run the script with the correct interpreter, e.g.:
& C:\cygwin\bin\bash.exe /cygdrive/c/script/path/script ...
Having cygwin installed with the ssh stuff and SCP utility you can simply issue:
C:\cygwin\bin\scp.exe /cygdrive/c/Users/YOU/Documents/file.txt root#remoteserver:/tmp/
I find above can marked solution becomes a bit too shortcoming as the bash profile is not quite the same as in a working cygwin bash environment.

Putting links to scripts in my cygwin bin

I have made a few python scripts, but is there an easier way to run them? I am using cygwin.
python "C:\Users\Desk\Dropbox\scripts\wsort.py" > data11414_unsorted.txt < data11414_sorted.txt
I want something like this (not typing the path name or "python"):
wsort > data11414_unsorted.txt < data11414_sorted.txt
where wsort is a link to my real wsort.py
Add a
Shebang
to the script
#!/bin/python
then invoke like this
wsort.py > data11414_unsorted.txt < data11414_sorted.txt
First, your question has a Windows-style path (backslashes, beginning with C:) rather than a Cygwin path (/cygdrive/c/Users/Desk/Dropbox/scripts/wsort.py). That implies you're not actually using Cygwin, or if you are, you're ignoring a bunch of warnings.
The below assumes you're using Cygwin Bash (which should be what you get if you start Cygwin Terminal from the Start Menu) and Cygwin Python (which you've installed using Cygwin's setup.exe, not a Windows Python installer). If your not, you're making life more difficult for yourself than you need to.
That out the way, there's a bunch of steps you need to take:
First, make the script executable. Use the chmod command for that, from a Cygwin Bash shell:
chmod +x /cygdrive/c/Users/Desk/Dropbox/scripts/wsort.py
Second, tell the system how to execute it. Add the following line to the top of the script:
#!/bin/python
(That's a "shebang". Python sees it as a comment, so doesn't do anything with it, but Cygwin and other Linux-like systems will use that line to see which program to run the script with. In this case, Python.)
Third, make sure your line endings are correct. Cygwin expects Linux line endings and will fail without them. This may not be a problem, but there's no harm in doing this. Run the following command:
dos2unix /cygdrive/c/Users/Desk/Dropbox/scripts/wsort.py
At this point, you'll be able to call the script by specifying the full path to it in Cygwin. You can't yet run it without specifying where the script is explicitly.
The fourth step is making sure the script is "in your path", ie in one of the folders where Cygwin looks for scripts to run. There are lots of ways to do this, but the most sensible is probably to just add your scripts directory to your path. The following command will add your scripts directory to your path whenever you start a new Cygwin session:
echo 'PATH="/cygdrive/c/Users/Desk/Dropbox/scripts:$PATH"' >>~/.bashrc
You will need to restart your Cygwin terminal for that to take effect, however.
At that point, you'll be able to run the script in Cygwin just by typing wsort.py (and thus use it with redirections and so forth as in your question).
Finally, to be able to call it simply as wsort, there's a number of options. The obvious one is just renaming the file. More usefully (and without copying the file or doing anything liable to break with Dropbox syncing things), try creating an alias:
echo 'alias wsort=wsort.py' >>~/.bashrc
Again, you'll need to restart your Cygwin terminal for that to take effect.
Maybe use an alias ?
alias wsort = "Command_Used"

bash script on cygwin - seems to get stuck between consecutive commands.

I am using a bash script to run a number of application (some repeatedly) on a Windows machine through cygwin. The script contains commands to launch those applications, line by line. Most of these applications run for many minutes and many times I have observed that the i+1 th application is not being started even after i th application is completed. In such cases, if I press enter in the cygwin console on which the bash script is running, the next application starts running. Is it because of any issue with bash on cygwin? Or is it an issue with the Windows OS itself? Have any of you observed such an issue with bash + cygwin + Windows?
Thanks.
I think I have seen this before.
Instead of
somecommand
try
somecommand </dev/null
If that doesn't work, try
cmd /c somecommand
Or experiment with other redirections, e.g.
somecommand >/dev/null
Sounds like you may have a problem with your shell script encoding; DOS (and Windows) uses CR+LF line endings, whereas Linux uses LF endings. Try saving the file as LF.
What might also be going on:
When I was running Cygwin on a school laptop, I encountered a dramatic slowing of shell scripts vs. when they were running in a native Linux environment. This was especially apparent when running a configure script from GNU Autotools.
Check your path for slow drives. (From the Cygwin FAQ):
Why is Cygwin suddenly so slow?
If suddenly every command takes a very long time, then something is probably attempting to access a network share. You may have the obsolete //c notation in your PATH or startup files. Using //c means to contact the network server c, which will slow things down tremendously if it does not exist.
You might also want to check whether you have an antivirus program running. Antivirus programs tend to scan every single executable file as it is executed; this can cause problems for even simple shell scripts that run hundreds or even thousands of individual programs before they run their course.
This mailing list post outlines what is needed to pseudo-mount the main /usr/bin directory as cygexec. I'm not sure what that does, but I found it helped.
If you're running a configure script, try the -C option.
Hope this helps!
Occasionally, I'll get this behaviour because I have accidentally deleted the 'she-bang' at the top of the script, that is, deleted the #!/bin/bash on the first line of the script.
It's even more likely for this to happen when a parent shell script calls a child script that has the she-bang missing!
Hope this helps.
A bit of a long shot, but I have seen some similar behaviour previously.
In Windows 2000, if any program running in a command prompt window had some of it's text highlighted by the cursor, it would pause the command running, and you had to press enter or clear the highlighting to get the command prompt to continue executing.
As I said, bit of a long shot, but accidental mouse clicks could be your issue...
Install cygwin with unix style line breaks and forget weird problems like that.
Try saving your script as "the-properly-line-broken-style" for your cygwin. That is, use the style you specified under installation.
Here is some relevant information:
https://stackoverflow.com/a/7048200/657703

Cygwin automatic script launch

Im trying to automatically run a script using Cygwin via CMD. I basically created a BAT file that goes to the directory and executes an .SH file. SH files are accosiated with Cygwin, and I tried something like "cygwin update.sh" in the command line. But all it really does is open Cygwin. I want Cygwin to automatically run the script file. Is there any easy way to do this, I've been trying to find but can't. Thank you!
You'll want to call the shell script with a particular shell, e.g. bash.
When having Cygwin open, call which bash to figure out where the binary is located. Cygwin also comes with tools that can convert paths between Cygwin and Win32 form, which is pretty helpful in cases like yours.
There is one other thing that may work, depending on your setup. There is an environment variable named PATHEXT which declares file extensions that are deemed "executable" by CMD. This can be used to your advantage, if Windows is configured so that the shell's "open" verb executes the correct shell for the file extension .sh (in your case).
Good luck.
From Cygwin Terminal, read man mintty. Try something like the following from a Windows Command Prompt:
c:\cygwin\bin\mintty --hold always --exec /cygdrive/c/path/to/bash/script.sh
I also found this!
http://rothmanshore.com/2011/01/26/kick-off-a-cygwin-script-from-a-windows-bat-file-with-a-different-working-directory/
I didn't quite understand it at first, but then it worked as I wanted it. Just if anyone knows, is there a way to make the script run without the CMD window open?? Thanks

Resources