Bash shell scripting: Difference between revisions
From Andreida
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
== Links == |
=== Links === |
||
* [http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html Bash Guide for Beginners] |
* [http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html Bash Guide for Beginners] |
||
* [http://misc.flogisoft.com/bash/tip_colors_and_formatting Colors for echo] |
* [http://misc.flogisoft.com/bash/tip_colors_and_formatting Colors for echo] |
||
== Parameters == |
=== Parameters === |
||
$$ PID of the process |
$$ PID of the process |
||
$? exit status |
$? exit status |
||
Line 13: | Line 13: | ||
$# parameter count |
$# parameter count |
||
== || and && - status based commands == |
=== || and && - status based commands === |
||
Syntax: |
Syntax: |
||
normal-command && if-succeeded || if-error |
normal-command && if-succeeded || if-error |
||
Line 32: | Line 32: | ||
So || and && will only work for the last command and will only work for the next command. |
So || and && will only work for the last command and will only work for the next command. |
||
== While on console == |
=== While on console === |
||
while true; do echo -n `date`;ls -l <file>; sleep 1; done; |
while true; do echo -n `date`;ls -l <file>; sleep 1; done; |
||
== parameter check == |
=== parameter check === |
||
<pre> |
<pre> |
||
if [ "$1" = "--help" ] || [ $# -ne 1 ]; then |
if [ "$1" = "--help" ] || [ $# -ne 1 ]; then |
||
Line 45: | Line 45: | ||
</pre> |
</pre> |
||
== do something depending on output/no output of a command == |
=== do something depending on output/no output of a command === |
||
<pre> |
<pre> |
||
#!/bin/bash |
#!/bin/bash |
||
Line 61: | Line 61: | ||
Use the double quote for the echo or you will lose all line breaks. |
Use the double quote for the echo or you will lose all line breaks. |
||
== get the absolute directory of the executing script == |
=== get the absolute directory of the executing script === |
||
#!/bin/bash |
#!/bin/bash |
||
DIR=$(cd `dirname $0` && pwd) |
DIR=$(cd `dirname $0` && pwd) |
||
echo $DIR |
echo $DIR |
||
=== Remove the first/last character === |
|||
First character: |
|||
echo super | sed 's/^.//' |
|||
Last character: |
|||
echo super | sed 's/.$//' |
Latest revision as of 23:22, 6 June 2023
Links
Parameters
$$ PID of the process $? exit status $0 call of the current script $1 1st parameter $2 2nd parameter $n etc... $* all parameters $# parameter count
|| and && - status based commands
Syntax:
normal-command && if-succeeded || if-error
Example:
rm /bla && echo ok || echo not ok;echo Done
Will normally output:
rm: cannot remove `/bla': No such file or directory not ok Done
Attention:
rm /bla && echo ok ; echo really ok || echo not ok ; echo shit
Will output
rm: cannot remove `/bla': No such file or directory really ok shit
So || and && will only work for the last command and will only work for the next command.
While on console
while true; do echo -n `date`;ls -l <file>; sleep 1; done;
parameter check
if [ "$1" = "--help" ] || [ $# -ne 1 ]; then echo my script v 0.1, it's me ! echo This script will do some cool stuff but I won't tell you what. echo Syntax: $0 \<file\> \<whatever\> exit 1 fi
do something depending on output/no output of a command
#!/bin/bash for file in ~/work/Sx* do cd $file out=$(cvsupdate) if [ "$out" != "" ]; then pwd echo "$out" fi done
Use the double quote for the echo or you will lose all line breaks.
get the absolute directory of the executing script
#!/bin/bash DIR=$(cd `dirname $0` && pwd) echo $DIR
Remove the first/last character
First character:
echo super | sed 's/^.//'
Last character:
echo super | sed 's/.$//'