Skip to content

Latest commit

 

History

History
54 lines (46 loc) · 1.27 KB

Bash.md

File metadata and controls

54 lines (46 loc) · 1.27 KB

Bash

Decode base64

echo some_base_64== | base64 --decode
# Or, to include the newline character
echo `echo some_base_64== | base64 --decode`

Delete all json files in dir when argument list is too long for rm -rf *.json

for f in *.json; do sudo rm -rf "$f"; done

Make 100 copies of a file with number added to the name

for f in {1..100}; do cp some-file.json some-file-$f.json; done

Read from file and print each line

while read hostname; do
  echo "host: $hostname"
done < hosts.txt

Read from file containing a list of curls and run each of them

while read curlCommand; do
  eval "$curlCommand"
done < curls.txt

Loop through all files matching a filename pattern (access_log*) in the current directory and execute a command against each

for file in access_log* ; do cat $file | grep GET ; done

for loop [@] vs [*] vs without

my_array=(foo bar)
for i in "${my_array[@]}"; do echo "$i"; done # treat each element as a seperate string
# foo
# bar
for i in "${my_array[*]}"; do echo "$i"; done # gather all elements into a single string
# foo bar
for i in "${my_array}"; do echo "$i"; done # only use first element
# foo

Exit bash script on error

set -e