Skip to content

Instantly share code, notes, and snippets.

@harrietty
Last active November 13, 2018 20:23
Show Gist options
  • Save harrietty/d1397befb4d2ad234c8c6a45cbc9143a to your computer and use it in GitHub Desktop.
Save harrietty/d1397befb4d2ad234c8c6a45cbc9143a to your computer and use it in GitHub Desktop.

Update all extensions of a particular type of file

#!/bin/bash

# changes the extension of all files matching a given extension

extension_to_replace="$1"
new_extension="$2"

for file in *"$extension_to_replace"; do
  
  # "basename my_notes.md .md" will output "my_notes" without the extension
  base=$(basename $file $extension_to_replace)
  mv $file "${base}${new_extension}"
done

exit 0

Draw a line of n characters

#!/bin/bash

# Draws a line of a given length. A character to draw the line from may be provided

# Set the default character for the line

char="="
if [[ $1 ]]; then
  char="$1"
fi

# Check a sensible argument has been given for the number

num=10
if [[ ! $2 =~ [0-9]+  ]]; then
  echo "$2 is not a valid number"
  exit 1
else
  num=$2
fi

# Begin adding the character to the $line string, which begins as an empty variable

line=
for (( i=0; i<num; ++i )); do
  line="${line}${char}"
done

printf "%s\n" "$line"

exit 0

Print stdin to stdout disregarding any lines up until the line where the first instance of a given string is found

Option 1, when the filename to read from is given as an argument:

#!/bin/bash

# Prints each line of stdin after which a certain string is found

if [[ ! $1 ]]; then
  echo "No search string given"
  exit 1
fi

if [[ ! $2 ]]; then
  echo "No file given"
  exit 1
fi

searchterm=$1
file=$2

found=false

while read -r; do
  if [[ $REPLY =~ $searchterm || $found = true ]]; then
    echo $REPLY
    found=true
  fi
done < $file

exit 0

Another option, where the filename to read from is given from stdin:

$ ./stripto monday < notes.txt

#!/bin/bash

# Prints each line of stdin after which a certain string is found

if [[ ! $1 ]]; then
  echo "No search string given"
  exit 1
fi

searchterm="$1"

while read -r; do
  if [[ "$REPLY" =~ "$searchterm" ]]; then
    echo "$REPLY"
    break
  fi
done

# Continue reading from stdin where the previous loop broke

while read -r; do
  echo "$REPLY"
done

exit 0

An implementation of the head command

#!/bin/bash

# Prints the top 10 lines of a file, or n number of lines when number is provided

if [[ $# -gt 1 ]]; then
  num=$1
  filename="$2"
elif [[ $# -eq 1 ]]; then
  filename="$1"
  num=10
else
  echo "Not enough arguments"
fi

printed=0

while read -r; do
  if [[ $printed -lt $num ]]; then
    echo "$REPLY"
    printed=$((printed+1))
  fi
done < $filename

exit 0

An implementation of the tail command

#!/bin/bash

# Prints the top 10 lines of a file, or n number of lines when number is provided

if [[ $# -gt 1 ]]; then
  num=$1
  filename="$2"
elif [[ $# -eq 1 ]]; then
  filename="$1"
  num=10
else
  echo "Not enough arguments"
fi

# -s squeezes multiple spaces into 1 space, allowing us to cut on columns with a single space delimiter

total_lines=$(wc -l $filename | tr -s ' ' | cut -f2 -d ' ')

curr_line=1

while read -r; do
  if [[ $(( $total_lines - $curr_line )) -lt $num ]]; then
    echo $curr_line $total_lines
    echo "$REPLY"
  fi
  curr_line=$((curr_line+1))
done < $filename

exit 0

A random number guessing game

#!/bin/bash

# A random number guessing game

# Set a variable to a random number
declare -ir num=$(( ($RANDOM % 100) + 1 ))

# Initialize the user's guess
declare -i guess
guess=0

until (( $guess == $num )); do
  read -p "Take a guess: " guess

  echo "num is $num, guess is $guess"
  
  # If guess is 0, it means the user did not imput a number
  (( $guess )) || continue

  [[ $guess -lt $num ]] && echo "Higher!"
  [[ $guess -gt $num ]] && echo "Lower!"
  [[ $guess -eq $num ]] && echo "You guessed correctly! It was ${num}"
done

exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment