I know it, forget it and relearn it again. Time to write it down.
相关问题
- How to get the return code of a shell script in lu
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
To run a non-executable
sh
script, use:To run a non-executable
bash
script, use:To start an executable (which is any file with executable permission); you just specify it by its path:
To make a script executable, give it the necessary permission:
When a file is executable, the kernel is responsible for figuring out how to execte it. For non-binaries, this is done by looking at the first line of the file. It should contain a
hashbang
:The hashbang tells the kernel what program to run (in this case the command
/usr/bin/env
is ran with the argumentbash
). Then, the script is passed to the program (as second argument) along with all the arguments you gave the script as subsequent arguments.That means every script that is executable should have a hashbang. If it doesn't, you're not telling the kernel what it is, and therefore the kernel doesn't know what program to use to interprete it. It could be
bash
,perl
,python
,sh
, or something else. (In reality, the kernel will often use the user's default shell to interprete the file, which is very dangerous because it might not be the right interpreter at all or it might be able to parse some of it but with subtle behavioural differences such as is the case betweensh
andbash
).A note on
/usr/bin/env
Most commonly, you'll see hash bangs like so:
The result is that the kernel will run the program
/bin/bash
to interpret the script. Unfortunately,bash
is not always shipped by default, and it is not always available in/bin
. While on Linux machines it usually is, there are a range of other POSIX machines wherebash
ships in various locations, such as/usr/xpg/bin/bash
or/usr/local/bin/bash
.To write a portable bash script, we can therefore not rely on hard-coding the location of the
bash
program. POSIX already has a mechanism for dealing with that:PATH
. The idea is that you install your programs in one of the directories that are inPATH
and the system should be able to find your program when you want to run it by name.Sadly, you cannot just do this:
The kernel won't (some might) do a
PATH
search for you. There is a program that can do aPATH
search for you, though, it's calledenv
. Luckily, nearly all systems have anenv
program installed in/usr/bin
. So we startenv
using a hardcoded path, which then does aPATH
search forbash
and runs it so that it can interpret your script:This approach has one downside: According to POSIX, the hashbang can have one argument. In this case, we use
bash
as the argument to theenv
program. That means we have no space left to pass arguments tobash
. So there's no way to convert something like#!/bin/bash -exu
to this scheme. You'll have to putset -exu
after the hashbang instead.This approach also has another advantage: Some systems may ship with a
/bin/bash
, but the user may not like it, may find it's buggy or outdated, and may have installed his ownbash
somewhere else. This is often the case on OS X (Macs) where Apple ships an outdated/bin/bash
and users install an up-to-date/usr/local/bin/bash
using something like Homebrew. When you use theenv
approach which does aPATH
search, you take the user's preference into account and use his preferred bash over the one his system shipped with.To start the shell-script 'file.sh':
Another option is set executable permission using chmod command:
Now run .sh file as follows:
First, give permission for execution:-
chmod +x script_name
For running sh script file:-
sh script_name
For running bash script file:-
bash script_name
./script_name
NOTE:-you can check if the file is executable or not by using 'ls -a'
If you want the script to run in the current shell (e.g. you want it to be able to affect your directory or environment) you should say:
or
Note that
/path/to/script.sh
can be relative, for instance. bin/script.sh
runs thescript.sh
in thebin
directory under the current directory.For the bourne shell:
For bash:
The file extension .command is assigned to Terminal.app. Double-clicking on any .command file will execute it.