compsci-missing_semester_2020/_subsections/lesson-02.org
2025-07-16 22:10:36 +03:00

17 KiB
Executable file
Raw Permalink 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

grep

  • grep TEXT-TO-FIND LOCATION
  • this will recursively go through all files in a directory looking for "TEXT"

    grep -R  TEXT .

ripgrep

Key Features of rg (ripgrep):
  • Faster than grep (built with Rust and optimized for performance).
  • Ignores files listed in .gitignore by default.
  • Supports recursive searches automatically.
  • Better default output formatting.
  • Supports regex matching like grep -E.
  • Multithreaded searching for better performance on modern CPUs.
basic command structure
  • search recursively through all files for text

    rg "text"
  • case insensitive search

    rg -i "text"
  • show line numbers

    rg -n "text"
  • search files of the pattern shown

    rg "text" --glob "*.txt"
  • search through files that are classified under the type given

    • pattern example

      rg "text" -t txt
    • for example type 'txt' would search for

      • txt
      • md
      • rst
    • if you want to see the type list use
    rg --type-list
  • ask for a specified number of lines of context around search match

    rg "text" -C 3
  • dont ignore hidden files

    rg "text" -u
  • add stats to search

    rg "text" --stats

other alternatives

  • ack
  • ag

fuzzy finder

Overview of fzf

fzf is a fuzzy finder for the command line. It allows quick searching through lists (files, command history, processes, etc.) using an interactive interface. Unlike traditional search tools that require exact matches, fzf uses fuzzy matching, meaning you only need to type parts of what you're looking for, and it will find the closest match.

Why Use fzf?

  • Fast and efficient, even for thousands of files or lines.
  • Highly customizable, works with various CLI tools (find, grep, git, history, etc.).
  • Interactive, allowing navigation with arrow keys or Ctrl-N / Ctrl-P.
  • Minimal setup required, just install and start using it.

Test Commands to Try in fzf

Basic Usage
  1. Search files in the current directory

    fzf

    Opens an interactive search where you can start typing to filter through files.

  2. Search your command history

    history | fzf

    Lets you browse and select previously used commands.

  3. Find and open a file in Vim

    vim $(fzf)

    Searches for a file and opens it in Vim.

  4. Search running processes

    ps aux | fzf

    Lets you search for running processes.

  5. Find a Git-tracked file

    git ls-files | fzf

    Searches only files tracked by Git.

Advanced Usage
  1. Find and delete a file

    rm -i $(fzf)

    Select a file to delete (prompts before deletion).

  2. Search and kill a process

    kill -9 $(ps aux | fzf | awk '{print $2}')

    Finds a process and terminates it.

  3. Use fzf with ripgrep (fast file search)

    rg --files | fzf

    Searches only through files tracked by ripgrep.

  4. Preview file contents while searching

    fzf --preview "bat --style=numbers --color=always {}"

    Uses bat (a better cat) to preview file contents.

Customizing fzf

Set up a keybinding (Ctrl+T) to trigger fzf for file selection
bind '"\C-t": "fzf\n"'
Change fzf options to improve display
export FZF_DEFAULT_OPTS="--height 40% --layout=reverse --border"
  • height 40%: Shows results in a smaller window.
  • layout=reverse: Shows matches from the bottom up.
  • border: Adds a visible border.

Final Thoughts

  • fzf is a powerful tool for navigating files, commands, and processes.
  • Once installed, just type fzf anywhere in the terminal and start filtering.
  • Combine it with other commands for maximum efficiency.

tree

Overview of tree

tree is a command-line utility that displays directory structures in a tree-like format. It recursively lists files and directories, making it useful for visualizing folder hierarchies.

Why Use tree?

  • Provides a clear hierarchical view of files and directories.
  • Supports filtering by file type, depth, and patterns.
  • Can output results in various formats such as JSON and XML.
  • Useful for quickly understanding directory structures.

Test Commands to Try in tree

Basic Usage
  1. Display the directory structure from the current location

    tree

    Lists all files and folders in a tree format.

  2. Limit the depth of displayed directories

    tree -L 2

    Shows only two levels of the directory structure.

  3. Show hidden files in the tree

    tree -a

    Displays all files, including hidden ones (those starting with a dot).

  4. Display file sizes and permissions

    tree -h -p

    Shows human-readable file sizes along with permission flags.

  5. List only directories

    tree -d

    Displays only directories, excluding files.

Advanced Usage
  1. Output the tree structure to a file

    tree > structure.txt

    Saves the tree output to a text file for later reference.

  2. Generate JSON output

    tree -J

    Outputs the directory structure in JSON format.

  3. Filter by file extension

    tree -P "*.txt"

    Lists only `.txt` files within the directory structure.

  4. Exclude certain files or directories

    tree -I "node_modules"

    Hides the `node_modules` directory from the output.

Customizing tree

Set an alias for a commonly used tree command
alias t2="tree -L 2"

Creates an alias `t2` to quickly show two levels of the directory.

Change default colors in tree output
export TREE_COLORS="di=1;34:ln=1;36:fi=0;37"

Customizes the colors used in the tree display for directories, links, and files.

Final Thoughts

  • tree is a powerful tool for quickly understanding directory structures.
  • It is useful for documentation, debugging, and navigating complex file systems.
  • The command supports various output formats, filtering, and customization options.

broot

Overview of broot

broot is a command-line tool for navigating and managing directory structures efficiently. It provides a tree-like view of files and directories while allowing fuzzy searching, filtering, and file operations within the terminal.

Why Use broot?

  • Provides an interactive tree view of directories with better readability than `ls` or `tree`.
  • Allows fuzzy searching and filtering of files and directories.
  • Supports file operations (e.g., rename, delete, move) directly from the interface.
  • Can replace `cd` by letting you navigate and enter directories quickly.
  • Supports customizable keybindings and themes.

Test Commands to Try in broot

Basic Usage
  1. Launch broot

    broot

    Opens an interactive directory tree view.

  2. Navigate the file system with fuzzy search

    broot

    Start typing part of a file or directory name to filter results.

  3. Enter a directory from broot

    br

    If `br` is set up as a shell function, it allows `cd` replacement.

  4. Show hidden files

    broot --hidden

    Displays hidden files and directories.

  5. Only show directories

    broot --only-folders

    Filters out non-directory files from the view.

Advanced Usage
  1. Perform file operations within broot

    • Press `:rename` to rename a file.
    • Press `:delete` to remove a file.
    • Use `:move target_directory` to move a file.
  2. Find and open a file in an editor

    broot --cmd "edit file_name"

    Searches and opens a file using the default editor.

  3. Integrate broot with `cd` command

    broot --install

    Enables `br` to work as a replacement for `cd`.

  4. Customize broot settings

    broot --edit-conf

    Opens the broot configuration file for customization.

Customizing broot

Change default display options
broot --set-default-flags "gh"

Sets the default view to show hidden files and a grid layout.

Define a shortcut for frequently used commands
alias bcd='broot --cmd "cd"'

Creates an alias to quickly navigate directories.

Final Thoughts

  • broot is a powerful alternative to `ls`, `tree`, and `cd`, offering an interactive way to browse and manage files.
  • With fuzzy search and built-in file operations, it enhances productivity for command-line users.
  • Customization options allow tailored keybindings and themes for better usability.