
man command
/directory/subdirectory/file
cd path — change current directory
ls path — list current (specified) directory
pwd — show current directory
mkdir directory — create a new directory
rmdir directory — remove a (empty) directory
cat, less, more file — display file contents
vi, emacs, gedit file — edit file contents
cp path path — copy file or directory
mv path path — move (rename) file or directory
rm path — remove file or directory
diff path path — compare file or directory
chmod permissions path — change permissions
chown owner path — change (user) ownership
chgrp group path — change (group) ownership
ps — display running processes
kill — terminate running processes
bg, fg, nice — process control commands
shutdown time — system shutdown
reboot — system reboot
date — system date and time
time command — measures command timing
whereis command — find command instances
dmesg — system boot messages
> to create/overwrite with stdout, >> to append, < for stdin
echo "Test Message" > ~/file.txt
mail –s "Test Mail" johndoe < ~/file.txt
echo "Another problem!" >>logfile
ls | less
xargs command — uses stdin as arguments to command
find . -name temp.dat | xargs rm
set –o vi or set –o emacs
history — shows previous commands
sort — sort input line to output lines
-b : ignore white space
-f : case insenstive sorting
-n : compare fields as numbers
-r : reverse sort order
-k : specify sorting column
uniq — print unique lines (cull duplicates)
tee — copy input to two places
head, tail — print beginning or head of file
grep "pattern" — search for text
A shell script is a program. Therefore, it deserves all of the care that any other program should get, including, as appropriate:
Only an idiot would justify sloppy work with, “It’s only a shell script, so I didn’t bother doing …”.
Sure, if it’s a short-lived (you hope) program (whether script or not), then it may not require the full treatment. However, that decision is not determined by whether or not the program is a shell script, a Perl script, a Python script, or a C++ program.

Shell scripts are programs that:
Specific syntax that follows is for bash.
echo "string" — send string to stdout
read variable — read variable from stdin
pathname="/usr/lib"
$
echo $pathname
$0 — same as C’s argv[0]
$1 — same as C’s argv[1]
$2 — same as C’s argv[2]
$@ — same as "$1" "$2" "$3" …
$* — same as "$1 $2 $3 …"
$# — same as C’s argc-1
#! /bin/bash
# Make sure to remove all gophers here
usage() { echo "Usage: $0 <param>" >&2; }
# Spaces count!
if [[ $num -eq 1 ]]
then
echo "Number is one"
elif [[ $num -eq 2 ]]
then
echo "Number is two"
else
echo "Number is $num"
fi
==, !=, <, >, and =~ for strings
>= or <= for strings.
-eq, -ne, -lt, -le, -gt, and -ge for numbers
for datafile in *.zot
do
echo Now processing $datafile
done
let max=5
for ((i=0; i<max; i++))
do
echo $i
done
Didn’t we just say that < is for strings⁉
Why not $max?
Variables are strings; indicate numeric evaluation with: let a=2+2
% alpha=1
% beta=2 # no spaces around =
% gamma=$alpha+$beta
% let delta=alpha+beta
% echo $gamma
1+2
% echo $delta
3
You can also use $((…)), but it’s unsightly.
Somewhat limited, and certainly inelegant, capabilities compared to C:
% array=(one two three)
% echo ${array[1]}
two
% echo ${array[*]}
one two three
% echo ${#array[*]}
3
grep and vi commands
. — matches any single character
[chars] — matches any single character from given set
[^chars] — matches any single character not in given set
* — matches zero or more of what just came before
^,$ — matches the beginning & end of a line
\d is [0-9], \w is [A-Za-z0-9_],
\s is [ \f\t\n\r]
Beware of “levels” of regular expressions.
grep — most basic (BRE)
egrep — extended (ERE)
perl — superior (PCRE)
#! /bin/bash # # Go into a temporary “playpen” directory; clean it up when done. # If we can’t execute a file in TMPDIR, then change it to somewhere executable. script=$(mktemp -t playpen-script-XXXXXX) chmod u=rx,go= "$script" "$script" 2>&- || TMPDIR=~/tmp rm -f "$script" cd "$(mktemp -d -t playpen-XXXXXX)" # Create temporary dir. cp -r ~/.playpen/* . 2>&- # Files to play with chmod -R u+rw . # Works even if no files got copied. ls -lhog | grep -v '^total ' # Show what’s here. $SHELL chmod -R u=rwx . # Make everything removable. cd /tmp # Get away from temporary directory. rm -rf ~- # Remove previous, temporary, directory.
|
Modified: 2016-08-25T09:41 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |
|