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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-11-2006, 03:05 AM
|
#1
|
|
Member
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157
Rep:
|
passing command output to variable
Hi,
I tried a lot of ways to pass values to some variable i made up and it works partially, i can successfully do
TIME=12:55
printf $TIME
and to get the output '12:55', but i can't pass an output of a command 'hwclock' to the variable.. i tried 'TIME=$hwclock', 'TIME=hwclock' 'hwclock > TIME'
'$hwclock > TIME' but those wont work as i need them to.
How can i accomplish this?
Last edited by Infernal211283; 01-11-2006 at 03:07 AM.
|
|
|
|
01-11-2006, 03:11 AM
|
#2
|
|
Member
Registered: Jan 2005
Posts: 178
Rep:
|
Quote:
Command Substitution
Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed as follows:
$(command)
or
`command`
Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by `$', ``', or `\'. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with backslashes.
If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.
|
Quoted from bash (1)
Last edited by T.Hsu; 01-11-2006 at 03:17 AM.
|
|
|
|
01-11-2006, 03:26 AM
|
#3
|
|
Member
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157
Original Poster
Rep:
|
Quote:
|
Originally Posted by T.Hsu
When using the $(command) form, all characters between the parentheses make up the command; none are treated
specially.
|
executing 'TIME=$(hwclock)' worked partially, i mean, the output of the command is usually 'Wed 11 Jan 2006 11:17:44 AM IST -0.928094 seconds' but the value $TIME stored is only 'Wed' - the [space] between 'Wed' and '11' broke the string that was supposed to be in the variable which shouldn't happen according to the above, should it?
|
|
|
|
01-11-2006, 03:34 AM
|
#4
|
|
Member
Registered: Jan 2005
Posts: 178
Rep:
|
The problem is printf, use echo instead or printf %b "$(hwclock)" "\n"
|
|
|
|
01-11-2006, 03:41 AM
|
#5
|
|
Member
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157
Original Poster
Rep:
|
Superb!
echoing showed:
echo $TIME
Wed 11 Jan 2006 11:35:02 AM IST -0.263685 seconds
printfing showed:
printf %b $TIME
Wed11Jan200611:35:02AMIST-0.263685seconds
Exactly what i needed,
Thank you.
|
|
|
|
01-11-2006, 04:04 AM
|
#6
|
|
Member
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157
Original Poster
Rep:
|
ah, damn, guess it was to good to be true...
wanted to use it in a script like 'touch $TIME' to create files named as TIME's content but it wont work...
[root@fedint _tEsTiNg_]# touch $TIME
touch: invalid option -- 0
Try `touch --help' for more information.
:x
|
|
|
|
01-11-2006, 04:17 AM
|
#7
|
|
Member
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157
Original Poster
Rep:
|
being slow i can now say that i figured out that the error is because the file cannot be created with ':', so i turned to alternative command 'date' using it's formats (%y.%m.%d), now it all works, sorry for odd questions.
I appreciate you for your help.
|
|
|
|
01-11-2006, 04:19 AM
|
#8
|
|
Member
Registered: Jan 2005
Posts: 178
Rep:
|
double quotes
touch "$TIME"
|
|
|
|
01-11-2006, 04:35 AM
|
#9
|
|
Member
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157
Original Poster
Rep:
|
^ thanks..
|
|
|
|
01-11-2006, 04:37 AM
|
#10
|
|
Member
Registered: Jan 2005
Posts: 178
Rep:
|
Glad it helps. You may also try "touch -- $TIME" to see the results 
|
|
|
|
01-11-2006, 04:52 AM
|
#11
|
|
Member
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157
Original Poster
Rep:
|
Useful spamming command
[root@fedint 1]# echo $TIME
Wed 11 Jan 2006 11:35:02 AM IST -0.263685 seconds
[root@fedint 1]# touch -- $TIME
[root@fedint 1]# ll
total 36
-rw-r--r-- 1 root root 0 Jan 11 12:48 -0.263685
-rw-r--r-- 1 root root 0 Jan 11 12:48 11
-rw-r--r-- 1 root root 0 Jan 11 12:48 11:35:02
-rw-r--r-- 1 root root 0 Jan 11 12:48 2006
-rw-r--r-- 1 root root 0 Jan 11 12:48 AM
-rw-r--r-- 1 root root 0 Jan 11 12:48 IST
-rw-r--r-- 1 root root 0 Jan 11 12:48 Jan
-rw-r--r-- 1 root root 0 Jan 11 12:48 seconds
-rw-r--r-- 1 root root 0 Jan 11 12:48 Wed
|
|
|
|
01-11-2006, 05:26 AM
|
#12
|
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,392
Rep:
|
Just to enrich your learning about shell.
In general, the long format for date isn't useful for scripts and so. May be you prefer a more compact form of time. Something like this:
miguel@gold:~> date +%Y%m%d-%H%M%S
20060111-092317
so...
miguel@gold:~>FILENAME=$(date +%Y%m%d-%H%M%S)
miguel@gold:~>touch $FILENAME
or better yet:
miguel@gold:~>touch $(date +%Y%m%d-%H%M%S)
regards,
|
|
|
|
01-11-2006, 05:54 AM
|
#13
|
|
Member
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157
Original Poster
Rep:
|
yep, just what i thought after i saw how files look if they're named after 'hwclock', i'm already trying to make a script using all this neat knowledge, hopefully i'll manage to create something intelligent.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:18 AM.
|
|
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
|
|