Skip to content

Instantly share code, notes, and snippets.

@dbuscombe-usgs
Last active December 20, 2015 06:09
Show Gist options
  • Save dbuscombe-usgs/6083992 to your computer and use it in GitHub Desktop.
Save dbuscombe-usgs/6083992 to your computer and use it in GitHub Desktop.
Some common things I do in BASH
#Do something if a file exists
if [ -f $target_dir"/$target_file" ]
then do something here
fi
#Calculate pi using bc
pi=`echo "4*a(1)" | bc -l`
#Convert degs to radians
rad=`echo "$deg*($pi/180)" | bc -l`
#Calculate tangent in degrees
tandeg=$(echo "s($rad)/c($rad)" | bc -l)
#List subdirectories in pwd
subdir=$(ls -d */)
#Clear screen
printf "\033c"
#Calling python to do a math operation rather than bc , e.g. a ceil:
round_up=$(python -c "from math import ceil; print ceil($variable)")
#Pass arguments to script from command line
args=("$@")
first_arg=${args[0]}
second_arg=${args[1]}
#Pass number lines in file to variable
numrecords=$(wc -l $target_dir"/file.ext" | awk -F" " '{print $1}')
#Delete every file which is not a *.ext file
find $target_dir -maxdepth 1 -type f ! -iname "*.ext" | xargs -I '{}' rm '{}'
#Pipe the first two columns of file to new file
awk '{print $1 " " $2 " " (-$3) }' $xyzfile".dat" > $xyzfile"new.dat"
#Max and min of a column (n) in file
n=2
lims=$(awk 'BEGIN{ max = -999999999 ; min = 9999999999 } $n > max {max = $n} $n < min {min = $n} END{ print min, max }' $file)
#Find a replace in file (NaNs with Os)
sed -i 's/NaN/0/g' file.ext
#Select a directory using zenity and pass to variable
export my_dir=`zenity --file-selection --title="Select a Directory" --directory`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment