Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-23-2010, 11:22 PM
|
#1
|
LQ Newbie
Registered: May 2007
Posts: 18
Rep:
|
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
printf "File: %s\n" $1
var=`wc -l $1`
printf "Lines: %s\n" $var
I get this as output:
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.
|
|
|
01-23-2010, 11:28 PM
|
#2
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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
printf "File: $1\n"
printf "Lines: `wc -l $1`\n"
much simpler.
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
Last edited by GrapefruiTgirl; 01-23-2010 at 11:29 PM.
|
|
1 members found this post helpful.
|
01-24-2010, 01:22 AM
|
#3
|
Member
Registered: Jan 2010
Distribution: Arch, Gentoo, FreeBSD
Posts: 64
Rep:
|
I think you should remember to use $() in bash instead of backticks and here's why.
|
|
|
01-24-2010, 01:28 AM
|
#4
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by slightlystoopid
I think you should remember to use $() in bash instead of backticks and here's why.
|
Not sure who you were writing that to, but I chose to not get into that since the OP admitted to being very new to this. It's sometimes better to let them finish learning one thing, before telling them to do it another way.
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
Last edited by GrapefruiTgirl; 01-24-2010 at 01:30 AM.
|
|
|
01-24-2010, 02:07 AM
|
#5
|
Member
Registered: Jan 2010
Distribution: Arch, Gentoo, FreeBSD
Posts: 64
Rep:
|
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?
|
|
|
01-24-2010, 02:14 AM
|
#6
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
lol, yes it can be difficult searching something like Google, for excerpts from scripts :/ as it usually returns garbage 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
|
|
|
01-24-2010, 02:30 AM
|
#7
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by GrapefruiTgirl
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 ``.
|
Both sides of this discussion have merit. I actually thought of posting something like slightlystoopid's I think you should remember to use $() in bash instead of backticks and here's why but backed off for the reasons Sasha gives. In retrospect how about answering as Sasha did and adding a "By the way ..." with something like slightlystoopid's post?
|
|
|
01-26-2010, 12:43 PM
|
#8
|
LQ Newbie
Registered: May 2007
Posts: 18
Original Poster
Rep:
|
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 09:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|