Shell
#prompt> echo $PS1
#searches manpage names and descriptions for regular expressions suplied as arguments.
> apropos music
> stat -c '%A %h %U %G %s %y %n' /tmp/studio
> find /tmp/ -name test.cpp -printf '%m %n %u %g %t %p'
#I won't pay $5 for coffee.
> echo 'I won'\''t pay $5 for coffee.'
#/etc/shells
> chsh
#GigaHerz = 1.93
> printf '%10.10s = %4.2f\n' 'GigaHerz' 1.92735
#1 STDOUT, 2 STDERR
> command 1> messages.out 2> messages.err
# command 1> messages.out 2>&1
> command &> messages.out
# braces { } used to group commands together
# with the braces used for grouping, any other changes that you make will be made to your current shell instance.
# The subshell is almost identical to the current shell's environment, but after it returns, its changes are never made to main shell instance.
> { comand1; command2; }> all.out
> (comand1; command2) > all.out
# tee - read from STDIN and write to STDOUT & files
> command1 | tee /out.1 | command2
# $() encloses a command that is run in a subshell, and newlines in the output are replaced with $IFS, which is a space by default.
> rm $(find . -name '*.class')
Shorten this:
if [ $result = 1 ]; then
echo "Result is 1; excellent."
exit 0
else
echo "Uh-oh, ummm, RUN AWAY!"
exit 120
fi
into this:
[ $result =1 ] \
&& { echo "Result is 1; excellent."; exit 0; } \
|| { echo "Uh-oh, ummm, RUN AWAY!"; exit 120; }
standard output is buffered but standard error is unbuffered.
| or ! means do it anyway. Eg. in vi, q!. Without a !, the editor will complain if you havenot save your changes.
echo something >| output.file
<< says that we want to create such a temporary input source, and the following is just an arbitrary string to act as the terminator of the temporary input.
grep $1 <<\EOF #grep $1 <<'EOF'
...
EOF
$? will be set with a non-zero value if the command fails. But be aware that you onlyget one shot at reading the exit status.
somecommand;
if (($?)) ; then ...; else ...; fi
somecommand && ... || ...
# run cmd as deamon and ignore the SIGHUP
nohup cmd &
PROG=echo
HI=hello
$PROG $HI
for SCRIPT in /path/to/scripts/dir/*
do
if [ -f $SCRIPT -a -x $SCRIPT ]
then
$SCRIPT
fi
done
set, env, export -p : seeing all variable values.
$@, $* refers to all the arguments supplied on the command line.
"$*" gives the entire list inside one set of quotes, but "$@" returns not one string but a list of quoted strings, one for each argument.
for param in "$@"
do
...
done
${#} gives the number of parameters the script was invoked.
${#VAR} gives the length of the value in the variable VAR.
${VAR#alt} does a certain kind of substitution.
# the :- operator, says that if $1 is not set or is null then it will use what follows as the value.
VAR1=${1:-"defaultValue"}
# the := operator, is the same as :- operator. But the := will do an assignment as well as return the value on the right of the operator. The :- will do halfof that -- it just returns the value but doesn't do the assignment.
cd ${HOME:=/tmp} # HOME eq. /tmp
#:? giving an error message for unset parameters
VAR1=${1:?"error message"}
inside ${ ... } Action taken
name:number:number Substring starting character, length
#name Return the length of the string
name#pattern Remove (shortest) front-anchored pattern
name##pattern Remove (longest) front-anchored pattern
name%pattern Remove (shortest) rear-anchored pattern
name%%pattern Remove (longest) rear-anchored pattern
name/pattern/string Replace first occurrence
name//pattern/string Replace all occurrences
array_1=(first second third)
${array_1} # $array_1 eq. ${array_1}
a=$((2**8)) #eq. 256
let a/=5
# / is integer arithmetic, not floating point. Eg, 2/3 eq. 0.
if [ $# -lt 3]
then
...
fi
or alternatively:
if (( $# < 3))
then
...
elif (( $# > 3 ))
then
...
else
...
fi
if test $# -lt 3
then
...
fi
Option Description
-b File is block special device (for files like /dev/hda1)
-c File is character special (for files like /dev/tty)
-d File is a directory
-e File exists
-f File is a regular file
-g File has its set-group-ID bit set
-h File is a symbolic link (same as -L)
-G File is owned by the effective group ID
-k File has its sticky bit set
-L File is a symbolic link (same as -h)
-O File is owned by the effective user ID
-p File is a named pipe
-r File is readable
-s File has a size greater than zero
-S File is a socket
-u File has its set-user-ID bit set
-w File is writable
-x File is executable
FILE1 -nt FILE2
ls newer than (it checks the modification date)
FILE1 -ot FILE2
ls older than
FILE1 -ef FILE2
Have the same device and inode numbers (identical file, even if pointed to by different links)
if [ -r $FILE -a -w $FILE ]
# use parentheses to get the proper precedence, as in a and (b or c), but be sure to escape their special meaning from the shell by putting a backslash before each or by quoting each parenthesis.
if [ -r "$FN" -a \( -f "$FN" -o -p "$FN" \) ]
# don't count on short circuits in your conditionals.
# it is important to put quotes around the "$VAR" expression because without them you syntax could be disturbed by odd user input. (like sql injection)
# the -eq operator for numeric comparisons and the equality primary = (or ==) for string comparisons.
NumericStringMeaning
-lt < Less than
-le <= Less than or equal to
-gt > Greater than
-ge >= Greater than or equal to
-eq =, == Equal to
-ne != Not equal to
# Testing with Regular Expressions:
# Ludwig Van Beethoven - 05 - "Leonore" Overture, No. 2 Op. 72.ogg
if [[ "$CDTRACK" =~ "([[:alpha:][:blank:]]*)- ([[:digit:]]*) - (.*)$" ]]
then
echo Track ${BASH_REMATCH} is ${BASH_REMATCH}
mv "$CDTRACK" "Track${BASH_REMATCH}"
fi
# For built-in array variable $BASH_REMATCH, the zeroth element ($BASH_REMATCH) is the entire string matched by the regular expression. Any subexpressions are available as $BASH_REMATCH, #BASH_REMATCH, and so on.
#Matches are case-sensitive, but you may use shopt -s nocasematch (available in bash versions 3.1+) to change that. This option affects case and [[ commands.
while (( COUNT < MAX ))
do
...
done
while [ -z "$LOCKFILE" ]
do
...
done
while read lineoftext
do
...
done
#Any expression inside the parentheses is evaluated, and if the result is nonzero, then the result of the (( )) is to return a zero; similarly, a zero result returns a one.
# infinite loop
while ((1))
{
...
}
Eg.
$ svn status bcb
M bcb/amin.c
? bcb/dmin.c
? bcb/mdiv.tmp
A bcb/optrn.c
M bcb/optson.c
? bcb/prtbout.4161
? bcb/rideaslist.odt
? bcb/x.maxc
$
# delete ? files
svn status mysrc | \
while read TAG FN
do
if [[ $TAG == \? ]]
then
echo $FN
rm -rf "$FN"
fi
done
svn status mysrc | grep '^?' | cut -c8- | \
while read FN; do echo "$FN"; rm -rf "$FN"; done
for (( i=0, j=0 ; i+j < 10 ; i++, j++ ))
do
echo $((i*j))
done
for i in 1 2 3 4 5 6 7 8 9 10 # for i in $(seq 1 1 10)
do
echo $i
done
seq 1.0 .01 1.1 | \ #preferred approach for a really long sequence, as it can run the seq command in parallel with the while.
while read fp
do
echo $fp; other stuff too
done
case $FN in
*.gif) gif2png $FN
;;
*.png) pngOK $FN
;;
*.jpg) jpg2gif $FN
;;
*.tif | *.TIFF) tif2jpg $FN #Use |, a vertical bar meaning logical OR.
;;
*) printf "File not supported: %s" $FN
;;
esac
# get the content between line 555 and line 666 of file1.
sed -n "555,666"p file1
Well, that was then and this is now.
页:
[1]