LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script: saving output from command into a variable (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-saving-output-from-command-into-a-variable-784313/)

grcunning 01-23-2010 10:22 PM

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.

GrapefruiTgirl 01-23-2010 10:28 PM

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

slightlystoopid 01-24-2010 12:22 AM

I think you should remember to use $() in bash instead of backticks and here's why.

GrapefruiTgirl 01-24-2010 12:28 AM

Quote:

Originally Posted by slightlystoopid (Post 3838300)
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

slightlystoopid 01-24-2010 01:07 AM

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? :(

GrapefruiTgirl 01-24-2010 01:14 AM

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

catkin 01-24-2010 01:30 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 3838331)
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?

grcunning 01-26-2010 11:43 AM

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.