Advertisement
geofferey

My BASH lesson 7/24/2013

Jul 24th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. Them: typically ls sticks with its normal behavior unless you've been clever with aliases
  2. You: Can you resend your lest replies. I had to close chat for a sec
  3. Them: Sure. ls sticks with its normal behavior unless you've been clever with aliases
  4. Them: try changing "ls" to "\ls" (starting with a backslash) or "/bin/ls" (fully qualifying the path)
  5. Them: in a more permanent sense, you can switch from "ls" to "find" (find /your/path -name *.TERM)
  6. You: Okay give me just a min. Thanks for this help. I really appreciate it.
  7. Them: You're welcome. I'm glad to help, and also glad to flex my bash debugging skills. This otherwise might bite me too someday :)
  8. You: So we both get to learn. I just started scripting a couple weeks ago haha. Anyways. I ran then command like this
  9. You: #/bin/ls -F /var/tmp/screens/S-mobile/*.TERM | sort -t. -k1 -n | tail -n +2 | xargs -r rm
  10. You: I still get the same result.
  11. You: I'm going to try it with find now.
  12. You: I am running from root prompt now.
  13. You: It seems to work fine when I do it from standard prompt.
  14. You: Btw I am writing this for an iPhone lol
  15. You: find seems to have done the trick
  16. You: Let me test a lil more and make sure it's consistent
  17. You: Hell yeah! Seems the script can't accidentally create anymore screens. Your a life saver bro.
  18. Them: Oh sorry, stepped away for a sec
  19. Them: Glad to help though!
  20. You: I'm still checking but yeah it seems to work great. It seems find also helps a lot in situations like these. Glad you could help me.
  21. You: I'll probably hit you up if that's okay with more questions like the one I posted.
  22. Them: It's tricky--ls is usually sufficient but find is a little better with scripting.
  23. Them: I'd be glad to chime in if I see any more of your questions on SO
  24. You: What are the differences between the two commands. I've seen people use both to achieve the same results. I typically like ls tho.
  25. Them: ls is a little more-suited for users
  26. Them: find is a little more-suited for scripts--more options, more predictable output
  27. Them: (ls output is predictable too, but as you can tell, adjustments and hacks happen)
  28. You: I guess I'll start using find for my scripts then. I've had some issues with ls in the past as well. Would you like the see the script as a whole?
  29. Them: Sure, I guess. I'll update the script as listed, noting that find works.
  30. You: Cool. I just wanna see what a pro thinks of it and if I am doing things properly.
  31. Them: Oh, I'm no pro scripter. I don't write BASH for a living. :)
  32. You: Well you got more than me skills than me. I just know what I want to do. That's how I am able to find my answers. http://pastebin.com/sNBaKq6c
  33. Them: For what it's worth, know that you're abusing find the way you have it
  34. You: How so?
  35. You: I'm probably abusing a lot of things lmao
  36. Them: find typically takes a path and then some expressions to narrow it down. it then returns a set of files
  37. Them: unless i'm misunderstanding something, your shell would expand the fully qualified path you have
  38. Them: so by the time find sees it, it looks like "find /path/to/a /path/to/b /path/to/c"
  39. Them: which is okay i guess, it's just not giving find anything to do
  40. Them: it's like pulling out a big expensive sharp knife and then a bag of pre-sliced cheese... the job's already done :)
  41. You: Could it cause slowing in the script. I remember using find on another part of the script and it cause it slow to a crawl>
  42. You: & it's taking me a min the comprehend what you mean. I learn by trial and error, so in my eyes if it works it works. ;-)
  43. Them: possibly. i've never found "find" to be slow enough that i've had to benchmark it
  44. You: let me test and see how things get listed.
  45. You: yeah so when I rund find like theis
  46. You: # find /var/temp/screens/S-mobile it outputs
  47. You: /var/temp/screens/S-mobile/
  48. You: /var/temp/Screens/S-mobile/12459.TERM
  49. You: /var/temp/screens/S-mobile/13261.ttys01.Geofferey's-iPhone
  50. You: is that what you mean
  51. You: Cause it list like that.
  52. You: I like my big sharp expensive knife it works lol
  53. Them: well
  54. Them: i'm just saying that "xargs echo /path/to/*.TERM" would do the same thing
  55. You: How is find still slicing my cheese?
  56. Them: your shell is slicing your cheese
  57. Them: it's performing argument expansion against your wildcard
  58. Them: i think
  59. You: oh I see. so I could ditch the find part completely?
  60. Them: for the sake of demonstration, type "echo /*" ... echo has no idea about filename expansion, but your shell does
  61. Them: you could use either "find" or filename expansion...it's redundant to use both, is all i'm saying
  62. You: I'm getting a bit confused now. what part of the code is the file name expansion?
  63. Them: the asterisk in "*.TERM"
  64. You: the wildcard
  65. Them: yeah. read 3.4.9 on http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html
  66. You: So I can remove the wild card
  67. You: I think I'm gonna have you BASHing your head lol
  68. Them: Read my updated answer, that one uses find the way you'll typically see it
  69. Them: http://stackoverflow.com/a/17846459/1426891
  70. You: They both have the same output so I don't see the difference still.
  71. You: How about giving me an example on a different direcotry that would contain more files
  72. You: Here
  73. Them: Not saying the output would be any different, just saying that you were doing the same thing in two places.
  74. Them: I gotta bounce. Glad we got the script working :)
  75. Them: Have a good $TIME_UNIT !
  76. You: Alright man. Thanks for all the help. I really appreciate it and I will take what you said into account and do some more research & use the find command the way you specified.
  77. Them: good luck on your project. cheers!
  78. Start a new chat.
  79.  
  80. original thread: http://stackoverflow.com/questions/17846433/remove-files-base-on-integer-name/17846459#17846459
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement