LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 01-23-2010, 10:22 PM   #1
grcunning
LQ Newbie
 
Registered: May 2007
Posts: 18

Rep: Reputation: 1
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.
 
Old 01-23-2010, 10:28 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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 10:29 PM.
 
1 members found this post helpful.
Old 01-24-2010, 12:22 AM   #3
slightlystoopid
Member
 
Registered: Jan 2010
Distribution: Arch, Gentoo, FreeBSD
Posts: 64

Rep: Reputation: 16
I think you should remember to use $() in bash instead of backticks and here's why.
 
Old 01-24-2010, 12:28 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by slightlystoopid View Post
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 12:30 AM.
 
Old 01-24-2010, 01:07 AM   #5
slightlystoopid
Member
 
Registered: Jan 2010
Distribution: Arch, Gentoo, FreeBSD
Posts: 64

Rep: Reputation: 16
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?
 
Old 01-24-2010, 01:14 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 01-24-2010, 01:30 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by GrapefruiTgirl View Post
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?
 
Old 01-26-2010, 11:43 AM   #8
grcunning
LQ Newbie
 
Registered: May 2007
Posts: 18

Original Poster
Rep: Reputation: 1
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
csh Shell Script: Getting wc command output to variable to do math on it, how? vxc69 Programming 5 05-04-2009 04:31 PM
Write output or command to variable (shell) kernel_geek Programming 9 03-12-2007 04:45 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
shell scipting: append output of a command to a variable jonhewer Linux - Newbie 10 08-24-2005 05:42 AM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration