bash and command line Flashcards
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 ] ]