User Tools

Site Tools


journal:spring2019:jgallahe:week6

unix week6


March 5, 2019

These past few classes we have been covering vi extended, and creating our first scripts. with the extended vi, more settings can be created and saved to further personalize the awesomeness of the vi experience.

Advanced vi:

all commands preceded with :

.co copy

.m move

#,#s/char/relace/ search and replace the first instance of char and replace it with replacing from line # to #

%s/ above to the whole file

s/char/replace/g replace all instances of char above

r read in the file

n next file

N previous file

new opens a new file in the session split screen

      ^wj down
      ^wk up
      v vert split

split - split as a copy of the current

      ^wl left
      ^wh right
      v vert split

——————————————————————

General:

$(()) - Sets up a “pocket of arithmetic”

variables created inside of loops can be used after the loop

vi ^ move cursor to the beginning of the line

vi $ “ ” “ end ” “


Most of March 5 was spent in developing the below script for a binary number guessing game. Some of the key features are:

The if loop creation to allow for delimiters, elif for more ifs in conjunction, and else to say if all of the ifs fail what to do

the variable creation and setting

Use of different bash thing such as RANDOM

for loops to allow for the repeated running of a block of code

while loops to have the ability to repeat a block of code, but not having to

#!/bin/bash
##
## script3.sh - more fun with things and stuff
##
######################################################################
## Computer will make its selection (number between 1 to 100),
PICK=“$((${RANDOM}%100+1))”

## Set a min number hint MIN=“$((${RANDOM}%12+1))”

## Set a max number hint MAX=“$((${RANDOM}%12+1))”

## Set the max number of turns MAXTURNS=5

##Set status variable WIN=“false”

if [ ! -z “${1}” ]; then

      if [ "${1}" = "teehee" ]; then
              echo "psst, I picked the number ${PICK}"
      elif [ "${1}" = "eeheet" ];then
              echo "psst, I picked a number between $((${PICK}-${MIN})) and $((${PICK}+${MAX}))"
      elif [ "${1}" = "maxboi" ]; then
              MAXTURNS=1000
      fi

fi

##echo “psst, I picked ${PICK}”##Debugger

for ((turn=0; turn<${MAXTURNS}; turn++)); do

      chk=0
      while [ "${chk}" -eq 0 ]; do
              ## prompt the user to make a choice
              echo -n "(${turn}/${MAXTURNS}) Pick a number between 1 and 100: "
              read guess
              chk=$(echo "${guess}" | grep '^-\?[0-9][0-9]*$' | wc -l)
              if [ "${chk}" -eq 1 ]; then
                      if [ "${guess}" -lt 1 ] || [ "${guess}" -gt 100 ]; then
                              chk=0
                              echo "Invalid range"
                      fi
              else
                      echo "Invalid input"
              fi
      done
      ## Evaluate the user's guess againt the users pick
      if [ "${guess}" -gt "${PICK}" ]; then
              echo "Too high"
      elif  [ "${guess}" -lt "${PICK}" ]; then
              echo "Too low"
      else
              echo "Right on!"
              WIN="true"
              break
      fi

done

## Evaluate winning status if [ “${WIN}” = “true” ]; then

      echo "Congragulations, you won 3 shinies in ${turn} turn(s)"

else

      echo "You did not guess my number. You lose... Sorry, not sorry."

fi

exit 0

journal/spring2019/jgallahe/week6.txt · Last modified: 2019/03/05 21:18 by jgallahe