Logging into Cygwin and executing commands using bat file in windows - batch-file

I am using windows XP operating system and cygwin is installed in my C drive.
I need to login to cygwin directly to my directory path which contains a makefile and also a bash script called build.sh in the same directory. So i modified the original cygwin.bat file and added the line as shown below.
#echo off
C:
chdir C:\cygwin\bin
bash --login "/cygdrive/E/scheme_31july/build/build.sh"
When i double click on this bat file i could see my script executing but not on cygwin shell but on windows cmd shell as a result I get errors for "make" command like "No rule to make target" as make comes bundled with cygwin.
And when I explicitly login to cygwin using default cygwin.bat file and execute my script by giving following commands in cygwin shell the script executes without errors.
Basically I want to write a bat file so that I can keep it anywhere in my PC and instead of manually openeing the cygwin prompt and typing commands like:
$ cd /cygdrive/E/scheme_31july/build/
$ sh build.sh
it should happen automatically. I sit possible to do so.
Regards,
Harshit

No rule to make target sounds more like make being executed in the wrong directory. make itself seems to be available and running as intended.
Try this:
bash --login -c "cd /cygdrive/E/scheme_31july/build/ && sh build.sh"
This should start a --login session (which should give you access to all the settings and tools you'd expect in a cygwin prompt environment), then execute the given shell command, which is the cd and sh you asked for. You could also write those two lines to a separate script file, and pass the name of that to bash instead of the full path to build.sh.
You could also try to cd into C:\scheme_31july\build in the bat file and then execute bash from there. Not sure whether bash will try to change path upon entering the login session. You can try whether things work without the --login, both for this approach and the one above.
#echo off
C:
cd C:\scheme_31july\build
C:\cygwin\bin\bash.exe ./build.sh
I'm not sure whether you want the session to turn interactive after that or not. In the above case, bash will terminate after the script completed, and might even close the window. You might have to add a read into build.sh to avoid that. If you want bash to turn interactive after executing some command, you can try using the --rcfile option of bash to execute some commands and then turn interactive.

Related

How can I run batch in a shell?

I want to have a batch file that opens the command prompt, launches the iex shell within it and then starts my elixir program. The issue I'm having is that as soon as I invoke iex -S mix, which compiles the code and opens the elixir shell, then I am unable to write more commands into it.
:: Start iex and compile with mix
iex -S mix
:: Start elevators
Elevator.Supervisor.start
pause
The last part Elevator.Supervisor.start never runs, for some reason. I guess this is because I opened a shell within the command prompt. Is there a way to feed commands into the iex?
TL;DR use .iex.exs file which is loaded by iex upon start.
Create a file named .iex.exs in the project directory root with the content you want to be run:
Elevator.Supervisor.start()
remove any reference to elixir code (which is now located in .iex.exs) from your .bat file
run .bat file
enjoy.

How to run C program in Netbeans

I am trying to run the dmtx project from Git (link: https://github.com/dmtx/dmtx-utils) in Netbeans. I have configured the Project Properties in NetBeans and the project is building correctly.
When I run a particular C file (dmtxwrite.c) through bash it gives the correct output. As mentioned in the README, the correct way to run it in Bash is:
$ echo -n 123456 | dmtxwrite > image.png.
Now my question is how can I do the same using NetBeans?
Till now I only found answers related to passing command line arguments. However, that does not work.
how can I do the same using NetBeans?
Select your C project in the Projects panel.
Right-click it to open the context menu, then select Properties > Run > Run Command.
Enter your command to be run in the Run Command field.
This is the Help for Run Command in NetBeans 8.2:
Enter a command to be executed when you run the project. The IDE runs the command as /bin/sh -c "run-command" which enables you to use any shell syntax including redirection and pipes. Type the command in the Run Command field the same way you would run it from the command line. For example, you could type my-script -opt1 -opt2 to run a script and specify two options, or mycommand > output.log to run your command and send the output to a logfile. The Run Command list maintains history of previous entries so you can go back to the default value or select among several different commands you have entered.

'ls' is not recognized as an internal or external command, operable program or batch file

'ls' is not recognized as an internal or external command, operable program or batch file.
I get this error when I try to glance at the files of my folder.
cmd opened regularly(not as an admin).
I've recently downloaded anaconda for python.During the installation process, there was a time when I allowed to add a PATH which was not recommended.
'ls' used to work well be
Here are the paths
I'm fairly certain that the ls command is for Linux, not Windows (I'm assuming you're using Windows as you referred to cmd, which is the command line for the Windows OS).
You should use dir instead, which is the Windows equivalent of ls.
Edit (since this post seems to be getting so many views :) ):
You can't use ls on cmd as it's not shipped with Windows, but you can use it on other terminal programs (such as GitBash). Note, ls might work on some FTP servers if the servers are linux based and the FTP is being used from cmd.
dir on Windows is similar to ls. To find out the various options available, just do dir/?.
If you really want to use ls, you could install 3rd party tools to allow you to run unix commands on Windows. Such a program is Microsoft Windows Subsystem for Linux (link to docs).
We can use ls and many other Linux commands in Windows cmd. Just follow these steps.
Steps:
1) Install Git in your computer - https://git-scm.com/downloads.
2) After installing Git, go to the folder in which Git is installed.
Mostly it will be in C drive and then Program Files Folder.
3) In Program Files folder, you will find the folder named Git, find the bin folder
which is inside usr folder in the Git folder.
In my case, the location for bin folder was - C:\Program Files\Git\usr\bin
4) Add this location (C:\Program Files\Git\usr\bin) in path variable, in system
environment variables.
5) You are done. Restart cmd and try to run ls and other Linux commands.
you can use dir instead of ls in cmd
If you want to use Unix shell commands on Windows, you can use Windows Powershell, which includes both Windows and Unix commands as aliases. You can find more info on it in the documentation.
PowerShell supports aliases to refer to commands by alternate names.
Aliasing allows users with experience in other shells to use common
command names that they already know for similar operations in
PowerShell.
The PowerShell equivalents may not produce identical results. However,
the results are close enough that users can do work without knowing
the PowerShell command name.
when you use windows as operating system you should write dir
and you will find all folders including empty folders and their data bytes storage
and you can use git ls-files to show all folders but not including hidden folders
The reason you cannot run the ls command is because it does not exist on windows. The windows equivalent is the dir command, however it does not work the same way. The solution, Winls. It is
The ls command, written for windows.
According the the Winls github.
Had this error because i was using command prompt to access my files.
how did i solve it...
i opened my folder with vscode, and used bash from the terminal...the "ls" command worked as it should from bash.

Script via Plink in .bat behaves differently

I have a .bat file on my Windows machine. This .bat file uses plink.exe to connect to an Ubuntu machine and execute an .sh script. However, I get different behaviors on the script depending on how Plink is used:
log onto Ubuntu directly (in person) -- script succeeds
ssh via Bitvise client -- script succeeds
ssh via Plink (by calling plink.exe) and calling script from interactive shell (it's a Ubuntu shell within windows cmd.exe) -- script succeeds
ssh via .bat which then calls Plink -- script fails
The script fails w/ message:
error while loading shared libraries: libCint.so: cannot open shared object file: No such file or directory
Other posts seem to refer to installation/permission issues of libCint.so but I know this is not the case since the script works correctly in other instances as shown above.
Below is the plink.exe line from my .bat file:
plink.exe !plink_ssh_details! myscript
The above script fails when called this way via .bat file; again, note that it succeeds when called directly from the Ubuntu or when I -ssh directly into the Ubuntu via cmd.exe (using plink.exe) or Bitvise client. Any help would be appreciated.
In the other cases, you are using interactive sessions.
While the Plink uses non-interactive session by default, when you specify a command on its command-line.
Your script probably relies on some environment variables (like PATH) being set specifically.
It's quite probable that the variables are set only for interactive sessions. Probably because they are modified in a startup script that is executed (sourced) for the the interactive sessions only.
Solutions are:
Correct the startup scripts to modify the variables unconditionally (even for non-interactive sessions).
Modify the script not to rely on environment variables.
Or you can source the profile script, see Unable to run shell script with ktutil command from Windows using PLINK.
Force the Plink to use the interactive session using the -t switch
This is not a recommended solution, as using the interactive session to automate a command execution can bring you nasty side effects. See for example Is there a simple way to get rid of junk values that come when you SSH using Python's Paramiko library and fetch output from CLI of a remote machine?
Some more obscure SSH servers can also behave differently when "exec" channel is used to execute the command. See Executing command on Plink command line fails with "not found".
I had to hack a solution to work around this problem. Adding a "-i" option at the header of the bash script I was invoking from my .bat file did the trick:
#!/bin/bash -i
Note some warn of unwanted side effects (no mention of specifics tho...) when using this option. But calling this now interactive script from a remote ssh session (e.g. when using plink.exe from a Windows .bat file and passing inline commands to the Unix box) solves any issues regarding file/directory visibility & permission issues.
Note to plink users: if you're calling a script on Unix via plink and noticing that the script doesn't behave as expected...adding the "-i" may help debug/solve your problem. Again, note that some have claimed unwanted side-effects of this hack of which they/I'm unaware.

issue commands to window/process started from batch file

I want to pass commands to a process started in a batch file, specifically Cygwin. For instance, if I start Cygwin with something like the following:
start "window1" cmd.exe /c cygwin
How might I execute 'ls' in "window1", in the same batch file from which I started Cygwin?
I have to ask why you want to run the commands from a batch file? Do you want to take different actions based on the results/output of the commands?
In either case, what you are asking is bordering on impossible. Why don't you simply write your logic in a bash script and run
start "window1" cmd.exe /c c:\cygwin\bin\bash.exe -c [script]
where [script] is the path of your bash script.
Documentation on bash scripting is available at http://tldp.org/LDP/abs/html/
If you don't have cmd arg to cygwin to execute command in startup (like cmd.exe /K or /C) you can automate such thing with some script like AutoHotKey.

Resources