Topic 2: More Shell Skills Sub-Topic 1: Quoting Sub-topics: 1 quoting 2 shell variables 3 sub-shells 4 simple shell scripts (no ifs or loops yet) 5 bash initialization files 6 I/O redirection & pipes 7 aliases 8 text file formats Useful command: echo Prints all its arguments Normal rules: spaces separate words in commands some characters have special meanings to bash Sometimes useful to quote strings or characters. Three quoting mechanisms: 'string': every character in the string taken literally "string": most characters taken literally \c: character taken literally 1 2 Difference Between Single & Double Quotes Sub-Topic 2: Shell Variables A few special characters are interpreted by double quotes but not single quotes 1.!: history expansion 2. $: shell variables (next sub-topic) shell variables: a way to store information in bash To give a value to a variable: today=thursday If no "today" variables exists, creates it. Sets value to the string "Thursday" 3. back quotes (`) next week To refer to value of variable: use $. Example: echo $today 3 4
Uses of Shell Variables 1. Use as variables in shell scripts 2. Store settings for shell and other programs. To see all variables with values: set Referring to Shell Variables: More Awkward situation: ------$ object=computer ------$ echo I have two $objects I have two ------$ Why? Fix: ------$ echo I have two ${object}s I have two computers 5 6 Shell Variables and Quotes Shell Variables Are Temporary The $ character retains its special meaning inside double quotes. echo "Today is $today" Not inside single quotes. Changes & new variables only last for life of current shell. To make a permanent change: use an initialization file (soon!) 7 8
Useful Variables Set By bash $HOME: your home directory $PWD: current directory $SHELL: path name of current shell $USER: your user name PATH $PATH Contains list of directories separated by ":"s. When you type ls Bash looks for a command named "ls". Searching order: 1. Is it a built-in shell command? 2. In each directory in $PATH: is there a program called "ls"? 9 10 PS1 $PS1 is your bash prompt. Can contain literal characters plus some special ones: \d: the current date \h: the name of the host machine \j: number of jobs you have running \s: the name of the shell \u: name of user \w: current working directory \!: history number of this command Sub-Topic 3: Sub-Shells A shell is a program. Linux automatically runs the bash program when you open a terminal. You can run it too. One reason: experiment with settings. Command to start a sub-shell: bash Exit back to starting shell: exit Use Shell variable $SHLVL to see if you're in a sub-shell A sub-shell "inherits" many of your settings, including current folder, umask Does not automatically inherit variables or aliases export command: says variable should be inherited by sub-shells export myvar 11 12
Sub-Topic 4: Simple Shell Scripts Executing a Shell Script shell script = file containing bash commands First step: must be executable (chmod) Comments in scripts: # means rest of line is a comment Type command name alone: bash looks in your PATH directories Special variable names: $0: name of command $#: number of arguments $1,$2,...: the individual arguments $*: all the arguments in one string, separated by spaces Type command name with directory: no lookup needed 13 14 Complication: scripts & sub-shells Special Initial Comment When you type the name of a script as a command: executed in a sub-shell. Consequence: can't use to create aliases or set variables. If you want script to affect current shell: source myscript Executes commands from myscript in the current shell. #!/bin/bash At beginning of script, specifies this is a script to be run with bash. Why bother? Syntax of some commands different for every shell. Now your script will work for people using other shells. Not required for 220, but generally a good idea. 15 16
Sub-Topic 5: Bash Initialization Files In your home directory:.bash_profile: executed when you start a login shell.bashrc: executed when you start up any other shell Use these files for your own settings (variables, etc.) Sub-Topic 6: I/O Redirection & Pipes Three standard files associated with any command or program: file 0: standard input: default is keyboard file 1: standard output: for regular output, default is screen file 2: standard error: for error messages, default is screen I/O Redirection can change the defaults for these three files. Advice: before you change, make a copy! don't remove calls to standard system files Sample initialization files in /cas/course/cisc220 17 18 Command For Examples Redirecting Standard Input Command to use as example: sort. Reads standard input and sorts to standard output. demo... (use ^d to end input) command 0< file Reads input from file, instead of keyboard. Shorter form: command < file example: sort < myfile 19 20
Redirecting Standard Output command 1> file Sends standard output of command to file, instead of screen. Shorter form: command > file Example: sort > file Example of redirecting standard input and output: sort < input > output Output Redirection: Existing Files Bash may object if you redirect to an existing file Depends on noclobber option. To view current options: set -o To set option: set -o noclobber To turn off option: set +o noclobber command > file: if file already exists, overwrite command >> file: if file already exists, append 21 22 Redirecting Standard Error Getting Rid of Output You Don't Want command 2>filename example: ls *.c 2>errs send to /dev/null Example: cmd produces error messages you want to throw away: ls *.c 2>/dev/null 23 24
Redirecting Both Output Files To 2 separate files: cmd 1>goodOutput 2>errOutput or: cmd >goodoutput 2>errOutput means file 2 is sent to same place as file 1 To the same file: cmd 1>outFile 2>&1 or: cmd 2>outFile 1>&2 Situation: Inside a script, using a command that reads from the standard input. You have some literal text you want to send to it. sort << END one two three four END "Here Documents" 25 26 Pipes Sub-Topic 7: Aliases cmd1 cmd2 Sends standard output of cmd1 to standard input of cmd2 Alias = easy way to create synonym for a command provides shorthand for something you do often alias ll="ls l" Examples: command less command sort command sort > output command 2>&1 command2 cmd1 cmd2 cmd3 Also a way to turn on certain options always. Example from standard.bashrc file: alias rm="rm -i" To execute original program (without alias): quote the name "rm" file1 file2 To cancel an alias: unalias rm To see all your aliases: alias (no arguments) 27 28
Interesting Aliases Set For You In CASLab Sub-Topic 8: Text File Formats rm="rm i" cp="cp i" mv="mv i" ls="ls F" interactive mode asks before overwriting file asks before overwriting file adds flags at end of file names Basic text file format: ASCII or Unicode characters Problem: Different OSs, different character(s) for end of line! Unix/Linux: '\n' (newline) Windows/Dos: '\r' + '\n' (carriage return + newline) Mac: varies with version & program Most programs can deal with any of these formats. Others are confused by some formats. 29 30 Historical Note Useful Tools od ("octal dump"): listing of bytes in a file default or -o: each pair of bytes as an octal number -c: each byte as a character -x: each pair of bytes as a hexadecimal number -d: each pair of bytes as a decimal number wc ("word count"): number of lines, words & characters in a file 31 32
Conversion Programs On CASLab Linux: use fromdos and todos: changes arguments (make a copy first!) no effect if file already in desired format Windows: Notepad++ "EOL Conversion" in Edit menu 33