Skip to main content

Posts

Showing posts from 2020

How to Pretty Print JSON and XML from the Command Line

As developers we often need to take minified JSON and XML and pretty print it so it is human readable. A multitude of websites provide a free service where you can enter minified JSON or XML and it will format it the data so it is human readable. The problem is this can lead to leaking data causing security incidents. The solution is to write a couple of bash scripts to pretty print. A big thanks to Campovski 's answer on stackoverflow for the JSON and Ben Noland 's answer on stackoverflow and Anton I. Sipos ' comment for the XML I took these answers a little further and created a bash script that accepts arguments so you can pretty print A file The result returned form an API A string Data piped in Both scripts have the same usage Format a file with the -f flag JSON ./pretty_json.sh -f <name_of_file> XML ./pretty_xml.sh -f <name_of_file> Format the return value of an API with the -u flag JSON ./pretty_json.sh -u www.api.com XML ./pretty_xml.sh -u

Creating Multiple Files for Testing

I recently needed to create a number of files to test uploading. Copying and pasting multiple times seemed like to much work, so I wrote the following script to do the hard work for me. create a file called create_files.sh and make is executable $ touch create_files.sh $ chmod 774 create_files.sh Then open create_files.sh and paste the following script for i in $(eval echo { $1 .. $2 }) do echo "Test file $i " &gt; file $i .txt done The script takes two arguments, the number to start at, and the number to finish. This is useful for cases when you create files then discover you want more. Example $ ./create_files.sh 5 9 $ ls -la drwxrwxr-x  ./ drwxrwxr-x ../ -rwxrwxr-- create_files.sh* -rw-rw-r-- file5.txt -rw-rw-r-- file6.txt -rw-rw-r-- file7.txt -rw-rw-r-- file8.txt -rw-rw-r-- file9.txt I also created a script to copy files, as I also wanted to test uploading images and other file types create a file called copy_files.sh and make is exe