Update article

This commit is contained in:
Raven Scott 2024-09-18 19:33:34 -04:00
parent b1b6cfd650
commit 6f69ec66a4
2 changed files with 32 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -915,6 +915,38 @@ await cmd('bash /home/scripts/check_exist.sh ' + sshSurfID).then(out => {
});
```
## The check_exist.sh script
```bash
#!/bin/bash
if [ $( docker ps -a | grep $1 | wc -l ) -gt 0 ]; then
echo "1"
else
echo "0"
fi
```
This Bash script checks if a Docker container with a specific name or ID (passed as an argument to the script) exists. Here's a breakdown of what each part does:
**`#!/bin/bash`**:
- This is the shebang line that tells the system to run the script using the Bash shell.
**`if [ $( docker ps -a | grep $1 | wc -l ) -gt 0 ]; then`**:
- This checks if there is any Docker container matching the argument (`$1`) provided when running the script.
- **`docker ps -a`**: Lists all containers, including both running and stopped ones.
- **`grep $1`**: Searches for the container name or ID specified by `$1` in the list of containers.
- **`wc -l`**: Counts the number of lines output by the `grep` command, i.e., the number of matching containers.
- **`-gt 0`**: Checks if the count is greater than zero, meaning there is at least one match.
**`echo "1"`**:
- If a container with the given name or ID is found, it prints `1`, indicating the container exists.
**`else`**:
- This handles the case where no matching container is found.
**`echo "0"`**:
- If no container matches, it prints `0`, indicating the container does not exist.
This step ensures that users cannot attempt to write to a non-existent container, which prevents potential errors and ensures smooth operation.
### Capturing User Input with a Modal