Custom console using git bash - windows

I understand that console applications using git bash invoke sh.exe but I wonder how they continue to use sh.
I see on console apps often you do a sh.exe --login. I would guess this creates a session somewhere and I would presume that the console application would execute commands against it somehow?
Essentially I switched from UNIX to Windows. I have tried a lot of console apps (best being console2 and just git-bash) but they still do not work for me well.
Ia m annoyed enough that I fancy trying to programming a simple shell in Java. (I am a Java developer by trade.)
Any help would be great thanks to understand this, looking but searchs with console or git-bash in google generate so much random noise.

If you are using one of the various Unix shells ported to Windows, you should be aware that most of these shells use some heuristic to make Windows look like Unix, either to build a compatibility layout or for user convenience.
For instance, the git command is probably stored in a git.exe file but your shell scripts imported from your Unix workstation all say git so the shell will lookup git.exe if it does not find git.
Nobe of these heuristics works perfectly, so you have to expect regular inconsistencies and disagreements, especially if your are mixing several Unix ports together.
As for the meaning of --login, it is synonymous to -l and its main effect is to decide which initialization files are read by bash on startup`
-l Make bash act as if it had been invoked as a login shell (see
INVOCATION below).

Related

Issues with basic commands in gitbash

I'm just getting started trying to use command line and gitbash, and I've run into some basic issues.
As I understand it from the lesson I'm trying to go through online, I can run gitbash on windows instead of standard command line I would find on Mac and Linux(?)
The blurb about it was:
"WINDOWS USERS: You might want to jump ahead to the Installations Assignment and use the "Git Bash" application you'll install there instead of using the default Command Prompt. That way you can be sure all the commands will be the same."
So I downloaded Gitbash via Railsinstaller but the first command, man/man ls, gives me this error.
Am I misunderstanding the function of Gitbash? Do I need to install something to add on or something else entirely?
Unfortunately, one of the notable faults of Git Bash is that it does not ship with a man command, so no, you are not misunderstanding anything. If you try to ls or cd or grep you'll see that it works fine.
Without going into too much background, Git Bash is a shell environment on Windows that emulates standard POSIX terminal behavior and makes some common Linux tools available. I'm not personally prone to the reasoning behind this decision, but I just double-checked and man (and man-db, etc.) are available in the ecosystem behind the tool that Git Bash derives from. If you want to fight for it, you might want to open an issue on the Git for Windows GH repository; Johannes Schindelin is the lead maintainer there and is very active.

Best place to hook path conversion in to Emacs under Windows

I use GNU Emacs on Windows with git-bash for unix tools like locate, grep and find. The git-bash tools spit out paths in the following form:
/c/path/to/file/file.txt
Emacs needs for functions like find-file, find-file-at-point or (with the helm-package) the function helm-find-file its paths in the form
c:/path/to/file/file.txt
I wrote a converter-function which converts the former into the latter and I hooked it via defadvice in expand-file-name. This works reasonable well (e.g. in my initial use case to make helm-locate cooperate with the git-bash locate).
However in some cases it does not work, like in interactive use of find-file-at-point. So my question is what is a more universal place to hook this converter in to make even interactive calls work properly?
jenesaisquoi's comment brought me to cygwin-mount.el. I tried it with gitbash - and it magically works!
The reason is that gitbash provides a similar mount command as cygwin does and gitbash's paths are cygwin paths as well.
Just make sure the gitbash binaries are in emacs' PATH environment variable.

What is the Bash file extension?

I have written a bash script in a text editor, what extension do I save my script as so it can run as a bash script? I've created a script that should in theory start an ssh server. I am wondering how to make the script execute once I click on it. I am running OS X 10.9.5.
Disagreeing with the other answers, there's a common convention to use a .sh extension for shell scripts -- but it's not a useful convention. It's better not to use an extension at all. The advantage of being able tell that foo.sh is a shell script because of its name is minimal, and you pay for it with a loss of flexibility.
To make a bash script executable, it needs to have a shebang line at the top:
#!/bin/bash
and use the chmod +x command so that the system recognizes it as an executable file. It then needs to be installed in one of the directories listed in your $PATH. If the script is called foo, you can then execute it from a shell prompt by typing foo. Or if it's in the current directory (common for temporary scripts), you can type ./foo.
Neither the shell nor the operating system pays any attention to the extension part of the file name. It's just part of the name. And by not giving it a special extension, you ensure that anyone (either a user or another script) that uses it doesn't have to care how it was implemented, whether it's a shell script (sh, bash, csh, or whatever), a Perl, Python, or Awk script, or a binary executable. The system is specifically designed so that either an interpreted script or a binary executable can be invoked without knowing or caring how it's implemented.
UNIX-like systems started out with a purely textual command-line interface. GUIs like KDE and Gnome were added later. In a GUI desktop system, you can typically run a program (again, whether it's a script or a binary executable) by, for example, double-clicking on an icon that refers to it. Typically this discards any output the program might print and doesn't let you pass command-line arguments; it's much less flexible than running it from a shell prompt. But for some programs (mostly GUI clients) it can be more convenient.
Shell scripting is best learned from the command line, not from a GUI.
(Some tools do pay attention to file extensions. For example, compilers typically use the extension to determine the language the code is written in: .c for C, .cpp for c++, etc. This convention doesn't apply to executable files.)
Keep in mind that UNIX (and UNIX-like systems) are not Windows. MS Windows generally uses a file's extension to determine how to open/execute it. Binary executables need to have a .exe extension. If you have a UNIX-like shell installed under Windows, you can configure Windows to recognize a .sh extension as a shell script, and use the shell to open it; Windows doesn't have the #! convention.
You don't need any extension (or you could choose an arbitrary one, but .sh is a useful convention).
You should start your script with #!/bin/bash (that first line is understood by execve(2) syscall), and you should make your file executable by chmod u+x. so if your script is in some file $HOME/somedir/somescriptname.sh you need to type once
chmod u+x $HOME/somedir/somescriptname.sh
in a terminal. See chmod(1) for the command and chmod(2) for the syscall.
Unless you are typing the whole file path, you should put that file in some directory mentioned in your PATH (see environ(7) & execvp(3)), which you might set permanently in your ~/.bashrc if your login shell is bash)
BTW, you could write your script in some other language, e.g. in Python by starting it with #!/usr/bin/python, or in Ocaml by starting it with #!/usr/bin/ocaml...
Executing your script by double-clicking (on what? you did not say!) is a desktop environment issue and could be desktop specific (might be different with
Kde, Mate, Gnome, .... or IceWM or RatPoison). Perhaps reading EWMH spec might help you getting a better picture.
Perhaps making your script executable with chmod might make it clickable on your desktop (apparently, Quartz on MacOSX). But then you probably should make it give some visual feedback.
And several computers don't have any desktop, including your own when you access it remotely with ssh.
I don't believe it is a good idea to run your shell script by clicking. You probably want to be able to give arguments to your shell script (and how would you do that by clicking?), and you should care about its output. If you are able to write a shell script, you are able to use an interactive shell in a terminal. That it the best and most natural way to use a script. Good interactive shells (e.g. zsh or fish or perhaps a recent bash) have delicious and configurable autocompletion facilities and you won't have to type a lot (learn to use the tab key of your keyboard). Also, scripts and programs are often parts of composite commands (pipelines, etc...).
PS. I'm using Unix since 1986, and Linux since 1993. I never started my own programs or scripts by clicking. Why should I?
just .sh.
Run the script like this:
./script.sh
EDIT: Like anubhava said, the extension doesn't really matter. But for organisational reasons, it is still recommended to use extensions.
I know this is quite old now but I feel like this adds to what the question was asking for.
If your on a mac and you want to be able to run a script by double clicking it you need to use the .command extension. Also same as before make file executable with chmod -x.
As was noted before, this isn't really that useful tbh.
TL;DR -- If the user (not necessarily the developer) of the script is using a GUI interface, it depends on what file browser they are using. MacOS's Finder will require the .sh extension in order to execute the script. Gnome Nautilus, however, recognizes properly shebanged scripts with or without the .sh extension.
I know it's already been said multiple times the reasons for and against using an extension on bash scripts, but not as much why or why not to use extensions, but I have what I consider to be a good rule of thumb.
If you're the type who hops in and out of bash and using the terminal in general or are developing a tool for someone else who does not use the terminal, put a .sh extension on your bash scripts. That way, users of that script have the option of double-clicking on that file in a GUI file browser to run the script.
If you're the type who primarily does all or most of your work in the terminal, don't bother putting any extension on your bash scripts. They would serve no purpose in the terminal, assuming that you've already set up your ~/.bashrc file to visually differentiate scripts from directories.
Edit:
In the Gnome Nautilus file browser with 4 test files (each with permissions given for the file to be executed) with stupidly simple bash command to open a terminal window (gnome-terminal):
A file with NO extension with #!/bin/bash on the first line.
It worked by double-clicking on the file.
A file with a .sh extension with #!/bin/bash on the first line.
It worked by double-clicking on the file.
A file with NO extension with NO #!/bin/bash on the first line.
It worked by double-clicking on the file...technically, but the GUI gave no indication that it was a shell script. It said it was just a plain text file.
A file with a .sh extension with NO #!/bin/bash on the first line.
It worked by double-clicking on the file.
However, as Keith Thompson, in the comments of this answer, wisely pointed out, relying on the using the .sh extension instead of the bash shebang on the first line of the file (#!/bin/bash) it could cause problems.
Another however, I recall when I was previously using MacOS, that even properly shebanged (is that a word?) bash scripts without a .sh extension could not be run from the GUI on MacOS. I would love for someone to correct me on that in the comments though. If this is true, it would prove that there is a least one file browser out there where the .sh extension matters.

Automation on Windows with Bash like syntax

Is there a way that we can write automation scripts in bash syntax and run it on Windows host (We can call the executable file .exe of Windows). The Windows batch syntax looks quite complex :D
Any suggestions are appreciated.
You can use cygwin or mingw sys for this.
They are both just BASH implementation available on WinXX (actually they are much more, but you need now only bash).
But there are some differences:
cygwin uses its own file system hierarchy, with Win drives mapped to a part. subdirs. All related to file names is more unix-style. There are some problems with passing pathnames to Windows programs.
MinGW is more Windows friendly, file paths are like in Windows, less problems with Windows native programs.
You should try yourself and choose what you need.
You could install cygwin and run bash.
You can get a win32 port of bash. Cygwin is enormous, but native windows bash and a few utilities can be had for a much smaller footprint.
Start with UnxUtils, which includes a sh based on zsh (it's quite slow, though).
If that's not enough you can get a win32 bash from some places, though most are older versions.

how to start a new session in a shell script in platforms other than Linux?

I have this problem:
I have a script A, and it calls another script B, but this script B must run in another session, it is an easy job to do in a C program with setsid
(), but I cannot find an equivalent shell command. There is a setsid shell
command in Linux, but there are no such commands in AIX and other UNIX
platforms. Can anyone give me some advice on how to do it in AIX and other UNIX platforms? Thank you.
The setsid() system call exists in FreeBSD and OpenSolaris, and is part of POSIX.1. So I would think that it should exist in anything that claims to be POSIX-compliant.
AIX is fully compliant with "one or more" of the POSIX standards, but I've never used it, so I can't comment on it directly. Since it's a vendor-supported operating system, I recommend you touch base with your vendor.
Now.. What do you mean by "an equivalent shell command"? What do you mean by "session" in the context of a shell script? If what you're looking for is a way to run a second shell script with a separate controlling terminal from the original script, I suggest you look at GNU Screen instead of system calls. Screen should be available for AIX.
If you have a shell script that currently works for you in Linux, and you're trying to port it to other platforms, then include the script in your question. Otherwise, we're flying blind.

Resources