SHARE
TWEET

Untitled

a guest Jun 19th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. set -o history
  3. date
  4. last=$(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //')
  5. echo "last command is [$last]"
  6.  
  7. case "1" in
  8.   "1")
  9.   date
  10.   last=$(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //')
  11.   echo "last command is [$last]"
  12.   ;;
  13. esac
  14.      
  15. Tue May 24 12:36:04 CEST 2011
  16. last command is [date]
  17. Tue May 24 12:36:04 CEST 2011
  18. last command is [echo "last command is [$last]"]
  19.      
  20. #!/bin/bash
  21. die() { echo >&2 -e "nERROR: $@n"; exit 1; }
  22. run() { "$@"; code=$?; [ $code -ne 0 ] && die "command [$*] failed with error code $code"; }
  23.  
  24. case "1" in
  25.   "1")
  26.   run ls /opt
  27.   run ls /wrong-dir
  28.   ;;
  29. esac
  30.      
  31. $ ./test.sh
  32. apacheds  google  iptables
  33. ls: cannot access /wrong-dir: No such file or directory
  34.  
  35. ERROR: command [ls /wrong-dir] failed with error code 2
  36.      
  37. trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
  38. echo "last command is $previous_command"
  39.      
  40. cmd=$previous_command ret=$?
  41. if [ $ret -ne 0 ]; then echo "$cmd failed with error code $ret"; fi
  42.      
  43. set -e
  44. trap 'echo "exit $? due to $previous_command"' EXIT
  45.      
  46. echo !!
  47.      
  48. echo "Last command run was ["!:0"] with arguments ["!:*"]"
  49.      
  50. echo this is a test
  51. echo !!
  52.      
  53. set -o history -o histexpand
  54.      
  55. #!/bin/bash
  56.  
  57. exit_trap () {
  58.   local lc="$BASH_COMMAND" rc=$?
  59.   echo "Command [$lc] exited with code [$rc]"
  60. }
  61.  
  62. trap exit_trap EXIT
  63. set -e
  64.  
  65. echo "foo"
  66. false 12345
  67. echo "bar"
  68.      
  69. foo
  70. Command [false 12345] exited with code [1]
  71.      
  72. #!/bin/bash
  73. set -x
  74. echo some command here
  75. echo last command
  76.      
  77. #!/bin/sh
  78. ./test.sh 2>&1 | grep '^+' | tail -n 1 | sed -e 's/^+ //'
  79.      
  80. echo last command
  81.      
  82. # This construct is needed, because of a racecondition when trying to obtain
  83.    # both of last command and error. With this the information of last error is
  84.    # implied by the corresponding case while command is retrieved.
  85.  
  86.    if   [[ "${?}" == 0 && "${_}" != "" ]] ; then
  87.     # Last command MUST be retrieved first.
  88.       LASTCOMMAND="${_}" ;
  89.       RETURNSTATUS='✓' ;
  90.    elif [[ "${?}" == 0 && "${_}" == "" ]] ; then
  91.       LASTCOMMAND='unknown' ;
  92.       RETURNSTATUS='✓' ;
  93.    elif [[ "${?}" != 0 && "${_}" != "" ]] ; then
  94.     # Last command MUST be retrieved first.
  95.       LASTCOMMAND="${_}" ;
  96.       RETURNSTATUS='✗' ;
  97.       # Fixme: "$?" not changing state until command executed.
  98.    elif [[ "${?}" != 0 && "${_}" == "" ]] ; then
  99.       LASTCOMMAND='unknown' ;
  100.       RETURNSTATUS='✗' ;
  101.       # Fixme: "$?" not changing state until command executed.
  102.    fi
  103.      
  104. # Because of a racecondition, both MUST be retrieved at the same time.
  105.    declare RETURNSTATUS="${?}" LASTCOMMAND="${_}" ;
  106.  
  107.    if [[ "${RETURNSTATUS}" == 0 ]] ; then
  108.       declare RETURNSYMBOL='✓' ;
  109.    else
  110.       declare RETURNSYMBOL='✗' ;
  111.    fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top