Autosave: 2023-03-16 08:33:27

This commit is contained in:
thomasabishop 2023-03-16 08:33:27 +00:00
parent ea6507a47e
commit 3717dbe7b0
2 changed files with 40 additions and 14 deletions

View file

@ -35,6 +35,7 @@ echo "The arguments provided are $@"
echo "The first argument is $1"
echo "The second argument is $2"
echo "Your name is $1 and you are $2 years old"
echo "There were $# arguments"
```
This outputs:
@ -45,6 +46,7 @@ The arguments provided are Thomas 33
The first argument is Thomas
The second argument is 33
Your name is Thomas and you are 33 years old
There were 2 arguments.
```
Key points:
@ -52,3 +54,31 @@ Key points:
- `$0` designates the script or function that is being executed
- `$@` designates a list of all the arguments that are passed to the script
- `$1...` designates each individual argument
- `$#` gives us a count of the number of arguments
## Passing options
Options differ from arguments in that they are prepended with `-` and they can be passed in any order. We use the `getops` program to parse options.
We can demonstrate this with a script that takes in a username and password as options:
```sh
while getopts u:p: option; do
case $option in
u) user=$OPTARG;;
p) pass=$OPTARG;;
esac
done
echo "user: $user / pass: $pass"
```
With the input `my-option-script.sh -u thomas -p password123` we would get the following returned:
```
user: thomas / pass: password123
```
Here we set up a `while` loop to parse the stdin and store it in the variable `option`. We run this through a case statement and individuate each option, storing them in dedicated local variables via the global `$OPTARG` variable
Note: using a colon after each option means that the script will expect the given option to have a value.

View file

@ -5,20 +5,22 @@ tags:
- shell
---
We know that `$PATH` is an [environment variable](Environmental%20and%20shell%20variables.md). It is an important one because it keeps track of certain directories. Not just any directories but the directories **where executables are found**.
# The `$PATH`
We know that `$PATH` is an [environment variable](/Programming_Languages/Shell/Environmental_and_shell_variables.md). This variable keeps track of directories **where executables are found**.
Whenever any command is run, the shell looks up the directories contained in the `PATH` for the target executable file and runs it. We can see this is the case by using the `which` command which traces the executable of bash commands. Take the `echo` program:
```bash
which echo
/home/trinity/.nvm/versions/node/v16.10.0/bin/npm
# echo: shell built-in command
```
Or `npm` :
```bash
which npm
/home/trinity/.nvm/versions/node/v16.10.0/bin/npm
/home/thomas/.nvm/versions/node/v19.4.0/bin/npm
```
By default the path will always contain the following locations:
@ -30,12 +32,14 @@ By default the path will always contain the following locations:
- `/bin`
- `/sbin`
All the inbuilt terminal programs reside at these locations and most of them are at `/usr/bin`. This is why they run automatically without error. If you attempt to run a program that doesnt reside at these locations then you will get an error along the lines of program x is not found in PATH.
All the inbuilt terminal programs reside at these locations and most of them are at `/usr/bin`. This is why they run automatically without error. If you attempt to run a program that doesnt reside at these locations then you will get an error along the lines of `program x is not found in PATH`.
## Structure of the PATH
```bash
/home/trinity/.nvm/versions/node/v16.10.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Python39/Scripts/:/mnt/c/Python39/:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/nodejs/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Users/thomas.bishop/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/thomas.bishop/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Users/thomas.bishop/AppData/Local/Programs/Hyper/resources/bin:/mnt/c/Users/thomas.bishop/AppData/Roaming/npm
/home/thomas/.nvm/versions/node/v19.4.0/bin:
/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:
/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
```
## Adding to the PATH
@ -52,7 +56,7 @@ This enables me to access the Chromium binaries from my terminal session (needed
For demonstration, lets add a users desktop directory to the PATH.
First we go to the `.bashrc` and add the `export` command. [Remember](https://www.notion.so/Environmental-and-shell-variables-04d5ec7e8e2b486a93f002bf686e4bbb) that this is the command for creating a new environment variable:
First we go to the `.bashrc` and add the `export` command. Remember that this is the command for creating a new environment variable:
```bash
export PATH="$PATH=:~/Desktop"
@ -70,11 +74,3 @@ Then we can check this directory has been added to the path with an echo
echo $PATH
...:~/Desktop
```
## Relation between commands and programs
Whenever we issue a command in bash we are really running an executable program that is associated with the command. After all, this is why when we create our own bash scripts we must run `chmod` to make them executables.
When we issue `./file.sh` we are running an executable program.
How come, however that when we use a program like `cd` or `npm` we dont have to type `./cd.sh` or `./npm.sh` ?