|
|
What does the $( ) notation mean in a Bash script?On page 329 (Hour 16) of your book Teach Yourself Unix in 24 Hours is the example: value=3 ; string="my horse Horace" test $value < $(echo $string | wc –c) test `wc –l filename` -ge 10000 If I try this with this code: #!/bin/sh/ value=3;string="my horse Horace" test $value < $(echo $string | wc -c) test `wc -l ched4` -ge 10000 I get:- syntax error at line 3: `(' unexpected Are you able to quickly explain what the line test $value < $(echo $string | wc -c) is actually trying to do? I don’t quite understand the $(…) part … and is the < performing a ‘redirection for input’ or is it meant to be ‘less than’? The $( ) notation is a modern replacement for the ` ` (also called the "backtick") notation, both submit the contents to a subshell for execution and replace it with the output of that command. For example: echo Today is $(date)
would produce the actual date in the string. Make sense? That’s what should be happening above. The notation then translates step by step: test $value < $(echo $string | wc -c)
then has the echo $string | wc -c replaced with the number of characters in the variable "string". That's going to be 15, so now we're testing: test $value < 15
since the variable "value" has been set to 3, we're really testing test 3 < 15
which should certainly be true! I notice two additional things in your example, though. First, you have /bin/sh/ with the trailing slash, which might be a problem. But you also might not be finding that you have Bash as your default shell on your system too, so do you want to try
#!/bin/bash
and see if that works. Let me know how it goes!
More Useful Unix and Linux Help Articles:
✔ Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔ Shell script to convert lowercase to title case?As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔ Can I script renaming files based on an XML data map?I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔ Test for valid numbers in a Bash shell script?In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔ Review: iSSH for the iPad/iPhoneIf you're running an online business like I am, there are times when you need to connect and log in to the server...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help
right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
Categorized:
Unix and Linux Help
(Article 7679,
Written by Dave Taylor)
Tagged: linux, shell script programming, unix Previous: Why does Internet Explorer block all my pop-up windows? Next: What cool things can I do with Twitter? Reader Comments To Date: 2TonyC said, on June 14, 2008 12:55 PM:
put a backslash before the < symbol in the test line, i.e.,
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
please let me know ,we type script in editor, i am using vi editor after that at command line we type :wq save and quit enter, we'll go to $ prompt then how to to see output for perticular script? is it shell dependent if yes let me know for borne,c-shell.please