shell scripting
[ condition to test ]
[ -e /etc/passwd ]
File operators
-d FILE - true if file is directory
-e FILE - true if file exists
-f FILE - true if file exists and is a regular file
-r FILE - true if file is readable by you
-s FILE - true if file exists and is not empty
-w FILE - true if file is writable by you
-x FILE - true if file is executable by you
String operators
-z STRING - true if string is empty
-n STRING - true if string is not empty
STR1=STR2 - true if strings are equal
Arithmetic operators
arg1 -eq arg2 - true if arg1 = arg2
arg1 -ne arg2 - true if arg1 <> arg2
-lt less than
-le less than or equal
-gt greater than
-ge greater or equal
-----------------------------
if a script doesn't contain a shebang the commands are executed using your shell
-------------------------------
#!/path/to/interpreter
VARIABLE_NAME="Value"
$VARIABLE_NAME
${VARIABLE_NAME}
VARIABLE_NAME=$(command)
-----------------------------
if [condition-is-true]
then
commands
elif [condition-is-true]
then
commands
else
commands
fi
-----------------------------
for VARIABLE_NAME in ITEM_1 ITEM_N
do
command 1
command N
done
----------------------------
every program returns exit status
$? - returns exit status
&& = AND #runs if previous command suceed
mkdir folder && cp file.txt /tmp
|| = OR #runs if previous command fails
cp test.txt /tmp/ || cp test.txt /tmp
----------------------------
functions...
[ -e /etc/passwd ]
File operators
-d FILE - true if file is directory
-e FILE - true if file exists
-f FILE - true if file exists and is a regular file
-r FILE - true if file is readable by you
-s FILE - true if file exists and is not empty
-w FILE - true if file is writable by you
-x FILE - true if file is executable by you
String operators
-z STRING - true if string is empty
-n STRING - true if string is not empty
STR1=STR2 - true if strings are equal
Arithmetic operators
arg1 -eq arg2 - true if arg1 = arg2
arg1 -ne arg2 - true if arg1 <> arg2
-lt less than
-le less than or equal
-gt greater than
-ge greater or equal
-----------------------------
if a script doesn't contain a shebang the commands are executed using your shell
-------------------------------
#!/path/to/interpreter
VARIABLE_NAME="Value"
$VARIABLE_NAME
${VARIABLE_NAME}
VARIABLE_NAME=$(command)
-----------------------------
if [condition-is-true]
then
commands
elif [condition-is-true]
then
commands
else
commands
fi
-----------------------------
for VARIABLE_NAME in ITEM_1 ITEM_N
do
command 1
command N
done
----------------------------
every program returns exit status
$? - returns exit status
&& = AND #runs if previous command suceed
mkdir folder && cp file.txt /tmp
|| = OR #runs if previous command fails
cp test.txt /tmp/ || cp test.txt /tmp
----------------------------
functions...
Comments
Post a Comment