Dies ist eine alte Version des Dokuments!
source script (or: . script)
Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged From man bash
exec command
replaces the shell. No new process is created (man bash) replaces the current (shell)-process with command (abs-guide)
command
when the shell encounters a command, it forks off a child process to actually execute the command (external commands only; from abs-guide) : Is this true? Isn't this
command &
?
eval command …
man bash: Command ist read and executed by the shell
For me useful if command contains spaces witch are protected by single quotes, eg wget -S --header'Content-Type: application/json'
. Without eval, --header'Content-Type:
and application/json'
would be interpreted as 2 single arguments
#!/bin/bash # first index is 0 app=( XMMS XINE XAWTV ) appcount=${#app[*]} # 3 appix=1 echo ${app[appix]} # XINE
while read -p 'Search term (English or German): ' inp test "$inp" do query $inp done for (( ix=10 ; ix<21 ; ix++ )) ; do echo $ix done for arg in "$@"; do # $@ sees arguments as separate words. echo $arg done # Print each word (separated by whitespace) in a textfile into one line (tested): for token in $(cat textfile); do echo $token done # Loop through an array... quote=('Die Mutter' 'der Dummen' 'ist immer' schwanger) # without index: for token in "${quote[@]}"; do echo $token done # with index: for ((i=0; i<${#quote[@]}; i++)); do echo "$i: ${quote[i]}" done
$meli is a shortcut for ${meli}
# get a value from a variable (meli) over a 'pointer' (who): (from abs-guide) meli=Melanie who=meli # who is used as pointer to meli adr=${!who} # now $adr contains Melanie # set a value into a variable (meli) over a 'pointer' (who): (tried myself) meli="" who=meli eval $(echo $who)=Melanie # now $meli contains Melanie
$# # number of command line arguments $* # Arguments as one word $@ # Arguments as separate words $$ # Pid of script $? # return value of command (& function?) $! # PID of last job run in background $0 # full name of script $1 # first argument of script
Following a shift
, the $@
holds the remaining command-line parameters, lacking the previous $1
, which was lost1).
aVar="Otto" echo -e "$aVar\nnext line" echo -e '$aVar\nnext line'
Result:
Otto next line $aVar next line
This means:
query() { echo $*; echo $@; echo $1 } query # call the function
Be aware of this:
if true; then echo "true=$?"; else echo "false=$?"; fi # prints true=0 ! if false; then echo "true=$?"; else echo "false=$?"; fi # prints false=1 !
Examples:
if [ -d $arg ]; then # a test (see below); mind the spaces! ... elif ! command; then ... elif [ "$a" == "$b" ] && [ "c" != "d" ]; then # also || ... elif [ -z "$aString" ]; then # true if length==0 ... fi
-d file True if file exists and is a directory. -e file True if file exists. -f file True if file exists and is a regular file. -h file True if file exists and is a symbolic link. -r file True if file exists and is readable. -w file True if file exists and is writable. -s file True if file exists and has a size greater than zero -x file has execute permission (for the user running the test) # Example: if [ ! -r "$sourcefile" ]; then echo "Error: Cant read $sorcefile!" exit 2 fi
command if [ $? -ne 0 ]; then echo "got an error!" fi #or: command if [ $? -eq 0 ]; then echo "command was successful" fi
abort
, my useful helper function:
abort () { if [ -n "$1" ]; then echo "$(basename $0): $1" >&2; fi if [ -n "$2" ]; then err="$2"; else err=1; fi exit $err } # usage examples: [ -d "$SOURCE" ] || abort "Source \"$SOURCE\" is not a directory" a_command || abort "Command failed" 7 a_long_command [ $? -eq 0 ] || abort "long command failed"
command > file.txt # stdout 2 file command 2> file.txt # stderr 2 file command 1>&2 # stdout 2 stderr command >&2 # stdout 2 stderr ? command 2>&1 # stderr 2 stdout command &> /dev/null # stderr & stdout 2 file (suppress output here)