bash and command line Flashcards
(31 cards)
redirect the output
something (1)> output.file
something (1)» output.file (append redirection, old content is not erased)
something &> output.file (for both output and errors)
variables in command line
$[var_name]
reading the input
read a >> hello $a = 'hello' read >> hi $REPLY = 'hi' read -p 'enter value: ' q >> enter value: 1 $q = 1
how to make a permanent variable
add to .bash_profile/.bashrc
redirect the error
something 2> output.file
grep
grep searchcontent searchsource
grep -i (case insensitive)
grep -v (excluding)
brace expansion
echo test{1,2,5}
» test1 test2 test5
echo test{1..6}
» test1 test2 test3 test4 test5 test6
permission
r - readable
w - writable
x - executable
-/d{rwx}{rwx}{rwx} = owner-group-other
change permission
chmod [u|g|0]=rwx file
chmod [+|-]rwx file
basic script structure
> > touch hello
nano hello
#! /bin/bash
echo ‘hello there’
> > chmod +x hello
./hello
‘hello there’
if condition
if [ some condition ]; then do something elif [ some other condition ]; then do this else do this instead fi
(non)empty string
-n|-z $var_string
number conditions
- eq ==
- ne !=
- lt <=
- gt >=
file conditions
- e exists
- d is a directory
- f is a regular file
- s not empty
- r is readable
- w is writable
- x is executable
logic conditions
and: -a [ [ cond && cond ] ]
or: -o [ [ cond || cond ] ]
for loop
for i in {range}; do
do something
done
for i in (( i = 0; i < 25; i = i+1 )); do
do something
done
source command
runs the script in the current shell
alias
alias [name]=’command’
unalias [name]
alias # show all aliases
command expansion/substitution
$(command)
parameters in a script
${1-999}
$# - number of parameters
$@ - all parameters
$* - all parameters (note: “$*” counts as one item like a string)
adding a script to the PATH
.profile
PATH=”path_to_script:${PATH}”
export PATH
functions
myfunc(){
}
bc computations
bc -q bc -l (default scale) scale=2 1/3=.33 scale(1.214)=3 sqrt(16)=4 quit
input redirection
command «_space;EOF
command «< ‘string’