Skip to content

Instantly share code, notes, and snippets.

@dtxe
Created January 19, 2024 14:01
Show Gist options
  • Save dtxe/57a0532b3b7ab911c27610832d2852b7 to your computer and use it in GitHub Desktop.
Save dtxe/57a0532b3b7ab911c27610832d2852b7 to your computer and use it in GitHub Desktop.
DSI Session 2 - Unix lesson 3 notes

Notes from Shell Lesson 3

Doing things faster in bash

Keyboard shortcuts

  • A good (albeit comprehensive) cheatsheet is found here: https://gist.github.com/tuxfight3r/60051ac67c5f0445efee
  • You'll eventually find a subset of the shortcuts that you use most often.
  • My personal list is:
    • Ctrl + A / Home key: move to the beginning of the line
    • Ctrl + E / End key : move to the end of the line
    • Ctrl + K : delete from cursor to end of the line
    • Ctrl + W : delete the current word
    • Ctrl + Left/Right arrow : move cursor faster (one word at a time)

Last command recall

  • Use the Up arrow (or Ctrl + P) to pull up previous commands
  • Type !! and press Enter to directly execute the last command
  • Use Ctrl + R to search your previous commands

Autocomplete (tab)

  • Typing a few characters then pressing your keyboard's TAB key autocompletes either:
    • the command (if you're typing a command)
    • files in the current directory (if you're providing a file argument to a command)
  • If there are multiple files that match what you've typed so far, press tab several times to see a list of options

Shell scripts

  • Several shell commands in a text file
  • Typically named with .sh file extensions
  • Run scripts by either:
    1. Setting them to executable with chmod +x myscript.sh, then executing them with ./myscript.sh
    2. Passing the script to bash as an argument (eg. bash myscript.sh)
  • Write scripts in a text editor (eg. VSCode or nano)
  • All bash scripts should start with #!/bin/bash on the first line to tell the computer what to use to run it

Adapting script behaviour based on context

Variables

  • Define variables with this notation:
    • Note: no space between variable name, equal sign, and assigned value
varname="varvalue"
  • Use variables with this notation:
    • Note: the $ is required when using bash variables
echo $varname

myfirstscript.sh

#!/bin/bash

# This is a comment
myvar="World"
echo "Hello $myvar!"

Parameters

  • Special variables named $1, $2, etc...
  • Set by bash to the arguments provided to the command
    • (not tested) Note on terminology:
      • A "parameter" is the name of the variable in the context of the function (eg. $1)
      • An "argument" is the value provided to the function / script that gets assigned to each parameter
      • Sometimes these two words are used interchangeably

Suppose we have a shell script named myparamscript.sh

#!/bin/bash
echo "Hello $1!"

Then we can change what it does when we run it in the shell

$ bash myparamscript.sh World
Hello World!

$ bash myparamscript.sh Bash
Hello Bash!

$ bash myparamscript.sh Everyone
Hello Everyone!

Parameter Expansion

Parameter names can be made unambiguous using curly braces. This notation is called Parameter Expansion.

#!/bin/bash

# This is a comment
myvar="World"
echo "Hello ${myvar}s!"

Parameter Expansion can also be used to substitute or set default values:

$ myvar=
$ echo ${foo:-"default value"}
default value

String operations

$ myvar="Toronto needs more trees" 
$ echo ${myvar:8:5}
needs

$ echo ${i//needs/must have}
Toronto must have more trees

Command substitution

  • Store output of a command into a variable
myvar=$(ls -1)
echo $myvar

Flow Control

&& and ||

  • command1 && command2 will run command1, then run command2 only if command1 was successful
    • eg. prepfiles && analyzefiles
  • command1 || command2 will run command1, then run command2 if command1 failed
    • eg. downloadfile || printerror

if/else

  • Perform different actions based on the value of a condition

while/until

  • Repeat actions while a condition is true or until a condition is true
#!/bin/bash
count=1
while [ $count -le 5 ]
do 
    echo $count
    count=$((count +1)) 

    mkdir "mynewdir$count"
    
done
echo "Finished."
#!/bin/bash
count=1
until [ $count -gt 5 ]
do 
    echo $count
    count=$((count +1)) 

    mkdir "mynewdir$count"
    
done
echo "Finished."

for

  • Run commands within the loop for each item in a variable / returned from a command

Putting it all together

  • This script renames all .txt files in a folder to .TXT
for i in $(find *.txt)
do 
    echo $i
    mv $i ${i//.txt/.TXT}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment