notes/IT/Scripting.md

34 lines
4.2 KiB
Markdown
Raw Normal View History

2024-05-30 18:01:43 +00:00
## Marking a script as executable
2024-05-30 18:02:43 +00:00
Sometimes, you may need to mark a script as an executable to run it. On Linux, this can be accomplished by `chmod +x [FILE]`, where `[FILE]` is the file you want to mark as executable. To unmark a file as executable, use `chmod -x`
2024-05-30 18:01:43 +00:00
2024-05-30 23:46:46 +00:00
## The Shebang
2024-05-30 18:02:43 +00:00
There are various shells with their own language syntax (`sh`, `bash`, `fish`, etc). Therefore, more complicated scripts will indicate a particular shell by specifying the absolute path to the interpreter as the first line, prefixed by `#!` like this:
2024-05-30 18:00:43 +00:00
```sh
#!/bin/sh
echo "Hello, World!"
2024-05-30 23:43:28 +00:00
```
2024-05-30 23:45:46 +00:00
`#!/bin/sh` means that this script can be executed by the binary located at `/bin/sh`, and so the reader knows this is a `sh`(ell) script.
2024-05-30 23:46:46 +00:00
## Variables
2024-05-30 23:48:46 +00:00
In bash, variable assignment is done with the `=` operator. Variables are conventionally named with `SCREAMING_SNAKE_CASE`, and can be accessed by prefixing the variable name with a dollar sign (`$`):
```bash
#!/bin/bash
2024-05-30 23:49:46 +00:00
# There may not be any spaces used during assignment, `A = B` means something different than `A=B`
2024-05-30 23:48:46 +00:00
# Assignment is done with the equal sign (=) operator:
2024-05-30 23:49:46 +00:00
MY_VARIABLE="Hi Mom!"
echo $MY_VARIABLE
2024-05-30 23:50:46 +00:00
```
2024-05-30 23:52:46 +00:00
## Quotes
In Bash, different styles of quotes mean different things:
2024-05-30 23:59:46 +00:00
| Quote | Description |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Single Quotes (') | Enclosing characters in single quotes (`'`) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash. Commands in single quotes will not be evaluated. |
| Double Quotes (") | Enclosing characters in double quotes (`"`) preserves the literal value of all characters within the quotes, with the exception of `$`, \`, `\`, and, when history expansion is enabled, `!`. The characters `$` and \ retain their special meaning within double quotes. A double quote may be quoted within double quotes by preceding it with a backslash. When the shell is in POSIX mode (see Bash POSIX Mode), the ! has no special meaning within double quotes, even when history expansion is enabled. |
| | |
2024-05-30 23:51:46 +00:00
## Commands
2024-05-30 23:52:46 +00:00
| Command | Description |
| ------------- | ---------------------------------------------------------------------------------------------- |
| `exit [CODE]` | Make the script process exit, where `[CODE]` is the exit code the process will terminate with. |
| | |