Bash

From Attie's Wiki
Revision as of 13:56, 5 July 2012 by Attie (Talk | contribs)

Jump to: navigation, search

Contents

Useful Arguments

-e treat any non-zero return as an error, and quit
-u treat any use of an unset variable as an error, and quit

example

#!/bin/bash -eu
 
echo "hi"
false
echo "there"

Command Not Found

function command_not_found_handle {
  echo "Hello"
  return 127
}

Line-by-Line

ls -l | while read -r line; do
  echo $line
done

Function List

Get a list of Perl functions, with their line number

cat gitweb.cgi | grep -n ^sub | sed -re 's/([0-9]+): ?sub ([^ {]+).*/\1:\t\2/' | sort -k2 | less

If conditions

Primary Meaning
[ -a FILE ] True if FILE exists.
[ -b FILE ] True if FILE exists and is a block-special file.
[ -c FILE ] True if FILE exists and is a character-special file.
[ -d FILE ] True if FILE exists and is a directory.
[ -e FILE ] True if FILE exists.
[ -f FILE ] True if FILE exists and is a regular file.
[ -g FILE ] True if FILE exists and its SGID bit is set.
[ -h FILE ] True if FILE exists and is a symbolic link.
[ -k FILE ] True if FILE exists and its sticky bit is set.
[ -p FILE ] True if FILE exists and is a named pipe (FIFO).
[ -r FILE ] True if FILE exists and is readable.
[ -s FILE ] True if FILE exists and has a size greater than zero.
[ -t FD ] True if file descriptor FD is open and refers to a terminal.
[ -u FILE ] True if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ] True if FILE exists and is writable.
[ -x FILE ] True if FILE exists and is executable.
[ -O FILE ] True if FILE exists and is owned by the effective user ID.
[ -G FILE ] True if FILE exists and is owned by the effective group ID.
[ -L FILE ] True if FILE exists and is a symbolic link.
[ -N FILE ] True if FILE exists and has been modified since it was last read.
[ -S FILE ] True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled.
[ -z STRING ] True of the length if "STRING" is zero.
[ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ] True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance.
[ STRING1 != STRING2 ] True if the strings are not equal.
[ STRING1 < STRING2 ] True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ] True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ] "OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.

Parse JSON

cat json_file | \
	# extract the top-most objects from the JSON
	fold -1 | \
	awk 'BEGIN{ depth=0; }
	          { if ($1 == "{") depth++;
	            else if ($1 == "}") {
	              depth--;
	              if (depth == 0) printf "}\n";
	            }
	            if (depth > 0) printf $1;
	          }
	       END{ printf "\n" }' | \
 
	# strip of the curly braces from each line
	sed -re 's/^\{//' -e 's/\}$//' | \
 
	# process line-by-line
	while read -r line; do
 
		# skip blank lines
		if [ "${line}" == "" ]; then
			continue
		fi
 
		# get a list of values/elements (take into account the posibility of a comma _inside_ a string
		items=$(echo "${line}" | \
			fold -1 | \
			awk 'BEGIN{ t = 0; q = 0; e = 0; }
			          { if (q > 0) {
			              if (!e && $1 == "\"") q--;
			            } else if (t > 0) {
			              if (!e && $1 == "'\''") t--;
			            } else if (!e && $1 == "\"") {
			              q++;
			            } else if (!e && $1 == "'\''") {
			              t++;
			            }
			            if (!q && !t && $1 == ",") {
			              printf "\n";
			            } else {
			              printf $1;
			            }
			            if ($1 == "\\") e = 1;
			            else e = 0;
			          }
			       END{ printf "\n" }')
 
		echo "${line}" | while read -r item; do
 
			echo "JSON Line: ${line}"
			value = $(echo "${line}" | \
			          fold -1 | \
			          awk 'BEGIN{ q=0;
			                    	e=0;
			                    	f=0;
			                    }
			                    { if (f) {
			                    		printf $1;
			                    	} else if (!e) {
			                    		if ($1 == "\"") {
			                    			if (!q) {
			                    				q = 1;
			                    			} else {
			                    				q = 0;
			                    			}
			                    		} else if ($1 == ":") {
			                    			f = 1;
			                    		}
			                    	}
			                    	if ($1 == "\\") {
			                    		e = 1;
			                    	} else {
			                    		e = 0;
			                    	}
			                    }' | \
			          sed -re 's/^"//' -e 's/"$//')
			echo "    Value: ${value}"
 
		done
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox