Presentation is loading. Please wait.

Presentation is loading. Please wait.

Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.

Similar presentations


Presentation on theme: "Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name."— Presentation transcript:

1 find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name "cs*" Two starting points: >find /usr /var/html –name "cs*" Case insensitive: >find /usr –iname "*htm*" More than 3 days ago: >find /usr -mtime +3 –name "*htm*" Within the last 3 days: >find /usr –mtime -3 –name "*htm*"

2 Customizing your prompt To set: set $PS1 variable with a prompt string. Additional prompt string capabilities: \d = todays date \H = host name \T = current time \w = working directory \! = history number of this command Example: export PS1=">" Example: export PS1="\! \w >"

3 Disk Usage Commands Free Blocks: >df Disk usage –Size of each directory (recursive): >du –Summary of each directory (not recursive): >du –s * –Size of a single directory: >du –s –Sort usage, numeric reverse, page at a time >du | sort –nr | more

4 Aliases Definition: Replace a string by a word at the start of a command Use: Shorten what needs to be typed by aliasing a command with its arguments Use: Rename a command to something more familiar Examples 1.Display all of the aliases in use: >alias 2.Example (dos user directory list): >alias dir='ls –l' 3.To remove an alias: >unalias dir Replace a word by a string when starting a command

5 Creating Script Variables create a variable: ="value" –Note: do not use spaces –Example: >TEAM="Yanks" >echo $TEAM –Variables are case sensitive –You can use variables anywhere in commnads >bindir=~bin >echo $bindir To make variables accessible to all sub shells, use export: >export TEAM="yanks"

6 Command Substitution Setting variables: ` ` or $( ) Examples 1.>dir=`pwd` >echo $dir 2.files=$(ls) echo $files 3.echo there are `grep –ci bush grades` bush\'s From command line: ` ` echo `date` grep "abc" `cat file.txt` The output of one command becomes part of another Note: The back quote is found on the top left of most keyboards Note: echo “$(ls)” preserves the new lines in the output

7 $PATH Environment Variable Most unix commands are C programs When you execute a command –$PATH contains a series of path names –The shell searches all variables in $PATH –To add program/bin to the $PATH variable export PATH = $PATH:program/bin –To add the current directory to $PATH export PATH = $PATH:.

8 Shell Scripts Put multiple commands in a file and then execute them by typing the file name –They execute the statements one by one Creating a script –The first line should be #!/bin/bash –Other lines starting with # are comments –Make executable: u+x scriptName –Execute:./scriptName [unless working directory is in the search path] Use script capabilities to –create loops –create decisions –write functions See Bash's Beginners guide See examples on the class web site

9 Arguments to a Shell Script Arguments to a script –$0 is the name of the shell script (argument 0) –$1 is the first argument –$2 is the second argument –$k is the k th argument –$# is the number of arguments –$@ is all of the arguments Example: scriptFile x y z –$0 = scriptFile –$2 = y –$# is 3 –$@ is x y z

10 Shell Script Decisions if [ condition ]; then fi if [ condition ]; then else fi if [ $1 == 3 ] ; then echo "arg equals 3" else echo "arg not 3"; fi Note: space after [ and before ], semi colon after ] Comparators: ! -lt –le –gt –ge –f == -r where -f is file –r for readable file

11 while and do Loops while [ condition ]; do command1; command2; command3 done i=0 while [ $i –lt 4 ]; do echo "Welcome $i"; i=$[$i+1]; done or i=0 do until [ $i == 4 ]; do echo "Welcome $i"; i=$[$i+1]; done or i=0 while [ $i –lt 4 ]; do echo "Welcome $i" i=$[$i+1] done Semicolons separate parts of the loop on the same line Boolean variables true or false are legal

12 for Loop for in ; do ; done; Example: for i in `cat list`; do cp "$i" "$i.bak;" done Example: for i in $PATHNAME/*; do rm $PATHNAME; done Example: >for x in *; do echo $x; done Note: the last example is directly from the command line

13 Calculate the average in a script #!/bin/bash if [ $# -lt 1 ]; then echo "usage: at least one argument needed" exit 1 fi value=0 count=0 for i in $@ do value=$[$value+$i] count=$[$count+1] done average=$((value/count)) echo $average average 1 outputs 1 average 1 2 3 outputs 2

14 Script Functions () { } Example: doSomething doSomething() { ls –l | more; } doSomething Note: Function must be declared before it can be used. Note: Spaces required before and after braces Note: A common error is forgetting the required spaces

15 Functions with arguments Arguments 1.$@ means all arguments 2.$* means all arguments, but treats quoted argments as one with embedded spaces 3.$0 is the command name 4.$1 first argument 5.$2 second argument 6.$# number of arguments Example: LL() { ls –lR $@ | more; } LL / Example: mycd() { cd $@; pwd; }

16 Shell Startups.bash_profile –configures the login environment –Most.bash_profiles also execute.bashrc.bashrc: configures subshell environments To customize a users environment, normally edit.bashrc include –Include aliases, variables, functions, search path ($PATH) –Good idea: Make the script in a test file first –Takes effect when logging in or executiong the source command.bash_profile and.bashrc in a user’s home directory


Download ppt "Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name."

Similar presentations


Ads by Google