compsci-missing_semester_2020/_subsections/lesson-02.org
2024-12-28 23:03:06 +02:00

8.2 KiB
Raw Blame History

lesson 02 | course overview + the shell

Notes

bash

spaces are critical with bash

  • this works:
foo=bar
echo $foo
  • this doesn't:
foo = bar
echo $foo
  • the output of that will be zsh: command not found: foo
  • what happens in the above example is that we are effectively calling the "foo" program with the arguments: "=" and "bar"

quotes

  • you can use double or single quotes to print a value

    echo "Hello"
    echo 'Hello'
  • double quotes can interpolate variables

    echo "Value is $foo"

    will return: Value is bar

  • single quotes can NOT interpoloate variables

    echo 'Value is $foo'

    will return: Value is $foo

sequential execution

bash functions

  • mcd.sh
mcd() {
        mkdir -p "$1"
        cd "$1"
        }
  • creates a function that can be executed after loading

    • $1 is a global variable referring to the first parameter

source

  • using source mcd.sh source mcd.sh
  • then carry out the function mcd testdir
  • this will create a new directory and cd into it as per the definied functioned

error codes

commands to access codes or values

  • $0 name of the script we are currently in

    • if you run this in the shell it will display 'bash'
  • $1->$9 the first through ninth argument given to a script
  • $? the last error message
  • $_ — last argument of the previous call
  • !! recreates the last call with it's arguments

    • use with sudo if you found your call needed a sudo and you don't feel like retyping it again
    ls -lah
    sudo !!

code values

  • boolean

    • true returns an error code of 0
    • false returns an error code of 1

using $@ and $*

  • represents all the arguments passed to a script or a function as separate, individual strings. It preserves each argument as a distinct item, even if the argument contains spaces.

key points about $@

  1. All Arguments: It refers to all the command-line arguments passed to the script.
  2. Preserves Spaces: If an argument contains spaces, it is treated as a single argument.
  3. Usage: It is commonly used in a loop to iterate over each argument.

example

./myscript.sh file1.txt "file with spaces.txt" file3.txt

then $@ will be file1.txt "file with spaces.txt" file3.txt

differences between $*

  • $@ treats each argument as a sepearte item
  • $* treats all arguments as a single, combined string

logical operators

  • you can use the error return value in conditionals

example using the OR conditional

false || echo "Oops fail"
"Oops fail"

example using the AND conditional

true && echo "this will print"
"this will print"

concatenate commands

  • you caoncatinate commands using the semicolon: ;

    false; echo "this prints always"
    "this prints always"

common substitution

  • take the output of a command and put it in a variable

    foo=$(pwd)
    echo $foo
    "/Users/ronny/..."
  • this can also be done by placing that format in a string and it will expand the string

    echo "the pwd output is: $(pwd)"
    "the pwd output is: /Users/ronny/..."

process substitution

description

Bash process substitution is a feature that allows you to use the output of a command or a process as if it were a file. It enables you to redirect input or output between processes in a flexible way without needing intermediate temporary files.

Syntax

  • creates a temporary file descriptor for the output of the command, which can be used as an input file in another command.

     <(command)
  • creates a temporary file descriptor for writing to the command, which can be used as an output file in another command.

     >(command)

examples

  • this will take b, a, d, c and sort it so the result is 'a, b, c, d'

    sort <(echo -e "b\na") <(echo -e "d\nc")
  • this will list the files, send the list to tee which will split a portion off to the screen and the rest to grep, followed by text_files.txt

    ls | tee >(grep "txt" > text_files.txt)
echo "Starting program at $(date)" # Date will be substituted
echo "Running program $0 with $# arguments with pid $$"

for file in "$@"; do
    grep foobar "$file" > /dev/null 2> /dev/null

    # when pattern is not found, grep has exit status 1
    # we redirect STDOUT and STDERR to a null register since we do not care about them

    if [[ "$?" - ne 0 ]]; then
        echo "File $file does not have any foober adding one"
        echo "# foobar" >> "$file"
     fi
done
  • $$ pid given for program
  • $# number of arguments
  • $@ expands to all the arguments

    • can be used in a for loop
  • 2> refers to STDERR
  • > refers to STDOUT

test utility

test is a bash utility that you can use to test the condition of a file. look at the man page for more info

curly braces

curly braces are used as a form of program command expansion.

the braces contain a number of arguments seperated by commans that will expand into arguments for the program

example

touch foo{,1,2,10}
touch foo foo1 foo2 foo10

cp foo{,.old}
cp foo foo.old

ranges

this takes the format {1..20}

touch directory{1..4}/foo{a..z}.txt

python scripts

making them bash executable

  • reference the python compiler at the top of the file

    • do it directly:

       #!/usr/local/bin/python
    • use the env program to do so

        #!/usr/bin/env python

args can be found through sys.argv[1:]

import sys
for arg in reversed(sys.argv[1:]):
  print(arg)

checking for problems in bash scripts using 'shellcheck'

shellcheck some_bash_script.sh
  • will tell you problems
  • and warnings

man command

  • works with both os, and installed tools
  • 'tldr' is easier to read

find