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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # September 24 - Conditionals
  2. ```
  3. $ man bash # open the man page for bash
  4. 1443g # go to line 1443 for conditional expressions
  5. ```
  6. ## Spaces in files and commands
  7. Example: a folder exists `"/tmp/foo bar"`
  8. Wrong: `ls /tmp/foo bar`
  9. Correct: `ls /tmp/foo\ bar`
  10. Also Correct: `ls "/tmp/foo bar"`
  11. ## Argument special characters
  12. ```
  13. $ test -z "hello"
  14. $# = 2
  15. $@ = "-z" "hello"
  16. $* = "-z hello"
  17. $0 = test
  18. $1 = -z
  19. $2 = "hello"
  20. ```
  21. ## Stdout vs Stderr
  22. ```
  23. echo "23" # 23 goes to stdout, 0 goes to stderr
  24. exit "23" # 23 goes to stderr, nothing goes to stdout
  25. ```
  26. ## I/O (input and output) Redirection and Piping
  27. the command doesn't care where stdin came from or where stdout is going
  28. ```
  29. $ ls > file.test # run ls and put the output in file.test
  30. $ sort < file.test # run sort with file.test as stdin
  31. $ cat realfile fakefile > cat_stdout 2> cat_stderr # 2> redirects stderr
  32. $ echo "new contents" >> file # append to file
  33. ```
  34. ## Edit a scratch file with EOF
  35. ```
  36. $ cat <<EOF | sort # edit a scratch file and pipe to sort
  37. >input
  38. >more input
  39. >EOF
  40. ```