Notes for Computer Information Technology 160
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sep21-conditionals.md 1.0KB

September 24 - Conditionals

$ man bash # open the man page for bash

1443g      # go to line 1443 for conditional expressions

Spaces in files and commands

Example: a folder exists "/tmp/foo bar"

Wrong: ls /tmp/foo bar

Correct: ls /tmp/foo\ bar

Also Correct: ls "/tmp/foo bar"

Argument special characters

$ test -z "hello"

$# = 2
$@ = "-z" "hello"
$* = "-z hello"
$0 = test
$1 = -z
$2 = "hello"

Stdout vs Stderr

echo "23" # 23 goes to stdout, 0 goes to stderr
exit "23" # 23 goes to stderr, nothing goes to stdout

I/O (input and output) Redirection and Piping

the command doesn’t care where stdin came from or where stdout is going

$ ls > file.test # run ls and put the output in file.test

$ sort < file.test # run sort with file.test as stdin

$ cat realfile fakefile > cat_stdout 2> cat_stderr # 2> redirects stderr

$ echo "new contents" >> file # append to file

Edit a scratch file with EOF

$ cat <<EOF | sort # edit a scratch file and pipe to sort
>input
>more input
>EOF