shell script: saving output from command into a variable
I thought I would learn shell scripting by trying something simple, but it hasn't turned out that way.
My plan is to use the wc command within my script to print some stats. if my script is script1, and I have a file myprog.c, then ./myscript myprog.c would print: File: myprog.c Lines: 16 Code:
#!/bin/sh File: myprog.c ./myscript: 4: 16: not found I'm guessing that I can't use the $1 within the backwards quotes, but I really need the wc command to operate on whatever file I pass as an argument. Any help would be appreciated. |
You can use variables within backticks (backward quotes) no problem but you seem to be overcomplicating what it is you're doing. Why not:
Code:
#!/bin/sh Note: the wc command also prints the name of the file being examined, so you will maybe want to add a argument to wc to make it only print the number of lines; or, use grep or sed or awk to grab the number and strip the filename. :) Sasha |
I think you should remember to use $() in bash instead of backticks and here's why.
|
Quote:
Yes, it's a good idea generally to use $(), but for someone starting to learn, with a tiny, simple example like this, backticks are OK. Plus, nobody mentioned bash anywhere. What if this isn't Bash? Realistically it probably is, but one never knows. Sasha |
well, it was towards the OP. If ya know wha cher doin, then ya know wha cher doin. ;) I still use them sometimes on the command line, I guess. I was just thinking it's good to start good habits early. Also, backticks have confuddled at least one script I've written. Someone on a forum then used $(), and I had to be like uhhh wut's a $(), google won't let me google "$()", what's the difference? :(
|
lol, yes it can be difficult searching something like Google, for excerpts from scripts :/ as it usually returns garbage :p but if you haven't tried it, Google-Code is pretty handy for finding scripts and header files and stuff like that, and you can use regex's if I remember right (normal regexes, as opposed to search-engine regexes).
As for the backticks, you're right that it's good to learn good habits early; very good point. $() is supported in *most* currently used shells, and never gets messy with escapes and quotes and things like this, the way `backticks` do. But, I figure, until the OP has a handle on doing what he wants to learn (that is, returning the value of a command, and printing it) I figured it best to adapt HIS example first. Once he understands that, then definitely, it'll be a good idea to check out FAQ82 which I think you linked, and understand the difference(s) between $() and ``. Best regards, Sasha |
Quote:
|
I've learned both now and I prefer $()
I've learned both methods now and I have to say that I prefer $() simply because there is no confusion like there can be for backticks. I very much like being told about both methods.
I am learning scripting because I will be taking CS474 (OS programming in Linux) in the summer and I like to be ahead of the curve. Thank you all so much for your help. I have learned a lot in the last few days. |
All times are GMT -5. The time now is 08:19 PM. |