This document provides a quick reference for the Bourne shell. It summarizes key components of the Bourne shell including variables, conditional tests, flow control structures like if/then, while and until loops, and case statements. It also covers I/O redirection, pipes, wildcards and basic scripting.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
32 views2 pages
Bourn Shell Ref
This document provides a quick reference for the Bourne shell. It summarizes key components of the Bourne shell including variables, conditional tests, flow control structures like if/then, while and until loops, and case statements. It also covers I/O redirection, pipes, wildcards and basic scripting.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
Bourne Shell Quick Reference Card II. Variables 3.
-w file file is writable
A. The statment name=value creates and assigns value to 4. -x file file is executable variable 5. -f file file exists and is a regular file SUM=12 6. -d file file is a directory B. Traditional to use all upper case characters for names 7. -c file file is a character special file C. Access content of variable by preceding name with $ 8. -b file file is a block special file I. Introduction to Shell Scripts echo $SUM 9. -p file file is a named pipe A. The shell is the program that you run when you log in D. Arguments go from right to left 10. -u file file has SUID set B. It is a command interpreter E. Results of commands can be assigned to variables 11. -g file file has SGID set C. There are three standard shells - C, Korn and Bourne SYS=`hostname` 12. -k file file has sticky bit set D. Shell prompts users, accepts command, parses, then F. Strings are anything delimited by "" G. Variables used in strings are evaluated 13. -z string length of string is 0 interprets command H. See example 3 14. -n string length of string is greater than 0 E. Most common form of input is command line input cat file1 file2 file3 I. System/standard variables 15. string1 = string2 string1 is equal to string2 F. Most commands are of the format 1. Command line arguments 16. string1 != string2 string1 is different from command [- option list] [argument Accessed by $1 through $9 for the first 9 command string2 list] line arguments. Can access more by using the shift 17. string string is not null G. Redirection and such command. This makes $1 .. $9 reference command 18. int1 -eq int2 integer1 equals integer2 1. < redirect input from standard input line arguments 2-10. It can be repeated to access a 19. int1 -ne int2 integer1 does not equal integer2 2. > redirect output from standard output long list of arguments. 20. int1 -gt int2 integer1 greater than integer2 3. >> redirect output and append 2. $# number of arguments passed on the command line 21. int1 -ge int2 integer1 greater than or equal to 4. | "pipes" output from one command to another 3. $- Options currently in effect (supplied to sh or to set integer2 ls -l | more 4. $* all the command line arguments as one long double 22. int1 -lt int2 integer1 less than integer2 5. tee "pipes" output to file and standard out quoted string 23. int1 -le int2 integer1 less than or equal to integer2 ls -l | tee rpt2 | more 5. $@ all the command line arguments as a series of 24. !condition negates (inverts) condition H. Entering commands double quoted strings 25. cond1 -a cond2 true if condition1 and condition2 are 1. Multiple commands can be entered on the same line if 6. $? exit status of previous command both true separated by ; 7. $$ PID ot this shell's process 26. cond1 -o cond2 true if either condition1 or 2. Command can span multiple lines if \R is typed at the 8. $! PID of most recently started background job condition2 are true end of each line except the last (R stands for 9. $0 First word, that is, name of command/script 27. \( \) used to group complex conditions carriage return, i.e. ENTER). This is escape sequence. I. Wild card characters can be used to specify file names in commands 1. * 0 or more characters 2. ? one character of any kind III. Conditional Variable Substitution V. Flow Control 3. [, , ] list of characters to match single character A. ${var:-string} Use var if set, otherwise use string A. The if statement B. ${var:=string} Use var if set, otherwise use string if condition J. Simplest scripts combine commands on single line like and assign string to var then C. ${var:?string} Use var if set, otherwise print string commands ls -l | tee rpt2 | more and exit else K. Slightly more complex script will combine commands D. ${var:+string} Use string if var if set, otherwise commands in a file use nothing fi 1. Use any text editor to create file, say my_sc B. Both the while and until type of loop structures are 2. Type commands into file, one per line (unless you use supported ; to seperate) while condition 3. Save file do 4. Make file readable and executable (more later on this) commands chmod a+rx my_sc IV. Conditional done 5. run script by entering path to file A. The condition part can be expressed two ways. Either as ./my_sc test condition We will make this a little easier later or until condition L. See examples 1 and 2 [ condition ] do where the spaces are significant. commands B. There are several conditions that can be tested for done 1. -s file file greater than 0 length 2. -r file file is readable C. The case statement is also supported 2. Useful for integer arithmetic in shell scripts i=`expr Examples case string in $i +1` pattern1) E. Executing arguments as shell commands commands 1. The eval command executes its arguments as a shell Single Line Script ;; command #! bin/sh pattern2) # Script lists all files in current commands directory in decending order by size ;;
esac VII. Shell functions ls -l | sort -r -n +4 -5