code_junkie

Can I pass an arbitrary block of commands to a bash function?

Nov 11th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. function conditional-do {
  2. if [ -f $1 ]
  3. then
  4. echo "Doing stuff"
  5. $2
  6. else
  7. echo "File doesn't exist!"
  8. end
  9. }
  10.  
  11. function exec-stuff {
  12. echo "do some command"
  13. echo "do another command"
  14. }
  15. conditional-do /path/to/file exec-stuff
  16.  
  17. function file_exists {
  18. if ( [ -e $1 ] ) then
  19. echo "Doing stuff"
  20. else
  21. echo "File $1 doesn't exist"
  22. false
  23. fi
  24. }
  25.  
  26. file_exists filename && (
  27. echo "Do your stuff..."
  28. )
  29.  
  30. file_exists filename && echo "Do your stuff..."
  31.  
  32. function file_exists {
  33. if ( [ -e $1 ] ) then
  34. echo "Doing stuff"
  35. shift
  36. $*
  37. else
  38. echo "File $1 doesn't exist"
  39. false
  40. fi
  41. }
  42.  
  43. file_exists filename echo "Do your stuff..."
  44.  
  45. [ -f $filename ] && echo "it has worked!"
  46.  
  47. function file-exists {
  48. [ "$1" ] && [ -f $1 ]
  49. }
  50.  
  51. file-exists $filename && echo "It has worked"
Add Comment
Please, Sign In to add comment