-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add gh-actions, bash, curl (#35)
- Loading branch information
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: 'Bash - declare and loop over array' | ||
author: julius | ||
date: '2023-08-24T19:05:00:00+01:00' | ||
tags: | ||
- 'bash' | ||
--- | ||
|
||
How to declare an array and loop over it? | ||
|
||
```bash | ||
# declare array: https://www.gnu.org/software/bash/manual/html_node/Arrays.html | ||
declare -a myArray=("Linux Mint" "Fedora" "Red Hat Linux" "Ubuntu" "Debian" ) | ||
|
||
# loop through it: https://www.freecodecamp.org/news/bash-array-how-to-declare-an-array-of-strings-in-a-bash-script/ | ||
for str in ${myArray[@]}; do | ||
echo $str | ||
done | ||
``` | ||
|
||
Sources: | ||
|
||
- declare array: https://www.gnu.org/software/bash/manual/html_node/Arrays.html | ||
- loop through an array: https://www.freecodecamp.org/news/bash-array-how-to-declare-an-array-of-strings-in-a-bash-script/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: 'Bash and curl - pass variables to curl' | ||
author: julius | ||
date: '2023-08-24T19:05:00:00+01:00' | ||
tags: | ||
- 'curl' | ||
- 'bash' | ||
--- | ||
|
||
I often call `curl` in a `bash` script. | ||
How do I pass variables that I've set in `bash` as arguments to `curl`'s command line option, say `--data`? | ||
|
||
Below is an example without variables. | ||
|
||
```bash | ||
curl \ | ||
--header "Content-Type: application/json" \ | ||
--data '{ | ||
"name": "Hello John Doe" | ||
}' \ | ||
http://example.com | ||
``` | ||
|
||
Splitting `John Doe` into two variables, and passing them to `curl` makes the server display the `greeting` as `"Hello $firstname $lastname"`. | ||
|
||
```bash | ||
# with variable - doesn't work. Variables are sent in request as is. | ||
# Server displays "Hello $firstname $lastname" | ||
$firstname="John" | ||
$lastname="Doe" | ||
|
||
curl \ | ||
--header "Content-Type: application/json" \ | ||
--data '{ | ||
"greeting": "Hello $firstname $lastname", | ||
"firstname": "$firstname", | ||
"lastname": "$lastname", | ||
}' \ | ||
http://example.com | ||
``` | ||
|
||
I've come across two cases: | ||
|
||
- If a variable is part of an already in a double quoted string, enclose in single quotes. | ||
See the value of key, `greeting`. | ||
- if a variable is to be its own json value, escape the double quotes. See `greeting`. | ||
See the value of key, `firstname` and `lastname` | ||
|
||
```bash | ||
$firstname="John" | ||
$lastname="Doe" | ||
|
||
curl \ | ||
--header "Content-Type: application/json" \ | ||
--data '{ | ||
"greeting": "Hello '$firstname' '$lastname'", | ||
"firstname": '\"$firstname\"', | ||
"lastname": '\"$lastname\"' | ||
}' \ | ||
http://example.com | ||
``` | ||
|
||
Thanks to [ Bernie Michalik's post](https://smartpeopleiknow.com/2021/11/12/if-you-are-writing-a-bash-script-to-call-a-curl-command-and-you-want-to-pass-variable-values-to-it-read-this) for the hints. |
36 changes: 36 additions & 0 deletions
36
bash/2023-08-24-bash-save-curl-response-code-and-body-in-variable.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: 'Bash - save curl response code and body in variables' | ||
author: julius | ||
date: '2023-08-24T19:05:00:00+01:00' | ||
tags: | ||
- 'curl' | ||
- 'bash' | ||
--- | ||
|
||
Save curl httpcode and response in variables for further processing. | ||
Found via https://unix.stackexchange.com/a/572434 | ||
|
||
```bash | ||
#!/bin/bash | ||
|
||
URL="https://example.com" | ||
|
||
response=$(curl --silent --write-out "%{http_code}" $URL) | ||
|
||
http_code=$(tail -n1 <<< "$response") # get the last line | ||
content=$(sed '$ d' <<< "$response") # get all but the last line which contains the status code | ||
|
||
echo "$http_code" | ||
echo "$content" | ||
|
||
# do something with the http_code, such as | ||
if [[ "http_code" -ne 201 ]]; then | ||
echo "Create failed." | ||
exit 1 | ||
fi | ||
``` | ||
|
||
Links to the command line options: | ||
|
||
- [`-s, --silent`](https://curl.se/docs/manpage.html#-s) | ||
- [`-w, --write-out`](https://curl.se/docs/manpage.html#-w) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Pass list to bash for processing | ||
|
||
Set list in a GitHub Actions workflow. | ||
And pass it to bash for processing, such as [looping through it](../bash/2023-08-24-bash-declare-and-loop-over-array.md). | ||
|
||
```yml | ||
name: my workflow | ||
on: | ||
workflow_dispatch: | ||
. . . | ||
jobs: | ||
jobA: | ||
# . . . | ||
steps: | ||
- name: Print List | ||
env: | ||
MY_LIST: '"a","b","c"' | ||
run: | | ||
bash .github/scripts/print-list.sh | ||
shell: bash | ||
``` | ||
Then, in bash, loop over the list and do something with it. | ||
```bash | ||
#!/bin/bash | ||
|
||
# file: .github/scripts/print-list.sh | ||
|
||
# check that env vars have been set | ||
if [[ -z "${MY_LIST:-}" ]]; then | ||
echo "ERROR: Missing env var MY_LIST" | ||
exit 1 | ||
fi | ||
|
||
# string processing via https://stackoverflow.com/a/35894538 | ||
for i in ${MY_LIST//,/ } | ||
do | ||
echo "$i" | ||
# remove first and last quotes: https://stackoverflow.com/a/9733456 | ||
i_without_quotes=$(sed -e 's/^"//' -e 's/"$//' <<<"$i") | ||
|
||
echo "$i" | ||
done | ||
``` |