Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- set -o history
- date
- last=$(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //')
- echo "last command is [$last]"
- case "1" in
- "1")
- date
- last=$(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //')
- echo "last command is [$last]"
- ;;
- esac
- Tue May 24 12:36:04 CEST 2011
- last command is [date]
- Tue May 24 12:36:04 CEST 2011
- last command is [echo "last command is [$last]"]
- #!/bin/bash
- die() { echo >&2 -e "nERROR: $@n"; exit 1; }
- run() { "$@"; code=$?; [ $code -ne 0 ] && die "command [$*] failed with error code $code"; }
- case "1" in
- "1")
- run ls /opt
- run ls /wrong-dir
- ;;
- esac
- $ ./test.sh
- apacheds google iptables
- ls: cannot access /wrong-dir: No such file or directory
- ERROR: command [ls /wrong-dir] failed with error code 2
- trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
- …
- echo "last command is $previous_command"
- cmd=$previous_command ret=$?
- if [ $ret -ne 0 ]; then echo "$cmd failed with error code $ret"; fi
- set -e
- trap 'echo "exit $? due to $previous_command"' EXIT
- echo !!
- echo "Last command run was ["!:0"] with arguments ["!:*"]"
- echo this is a test
- echo !!
- set -o history -o histexpand
- #!/bin/bash
- exit_trap () {
- local lc="$BASH_COMMAND" rc=$?
- echo "Command [$lc] exited with code [$rc]"
- }
- trap exit_trap EXIT
- set -e
- echo "foo"
- false 12345
- echo "bar"
- foo
- Command [false 12345] exited with code [1]
- #!/bin/bash
- set -x
- echo some command here
- echo last command
- #!/bin/sh
- ./test.sh 2>&1 | grep '^+' | tail -n 1 | sed -e 's/^+ //'
- echo last command
- # This construct is needed, because of a racecondition when trying to obtain
- # both of last command and error. With this the information of last error is
- # implied by the corresponding case while command is retrieved.
- if [[ "${?}" == 0 && "${_}" != "" ]] ; then
- # Last command MUST be retrieved first.
- LASTCOMMAND="${_}" ;
- RETURNSTATUS='✓' ;
- elif [[ "${?}" == 0 && "${_}" == "" ]] ; then
- LASTCOMMAND='unknown' ;
- RETURNSTATUS='✓' ;
- elif [[ "${?}" != 0 && "${_}" != "" ]] ; then
- # Last command MUST be retrieved first.
- LASTCOMMAND="${_}" ;
- RETURNSTATUS='✗' ;
- # Fixme: "$?" not changing state until command executed.
- elif [[ "${?}" != 0 && "${_}" == "" ]] ; then
- LASTCOMMAND='unknown' ;
- RETURNSTATUS='✗' ;
- # Fixme: "$?" not changing state until command executed.
- fi
- # Because of a racecondition, both MUST be retrieved at the same time.
- declare RETURNSTATUS="${?}" LASTCOMMAND="${_}" ;
- if [[ "${RETURNSTATUS}" == 0 ]] ; then
- declare RETURNSYMBOL='✓' ;
- else
- declare RETURNSYMBOL='✗' ;
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement