LinuxQuestions.org
Help answer threads with 0 replies.
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-11-2006, 03:05 AM   #1
Infernal211283
Member
 
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157

Rep: Reputation: 30
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.
 
Old 01-11-2006, 03:11 AM   #2
T.Hsu
Member
 
Registered: Jan 2005
Posts: 178

Rep: Reputation: 31
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.
 
Old 01-11-2006, 03:26 AM   #3
Infernal211283
Member
 
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157

Original Poster
Rep: Reputation: 30
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?
 
Old 01-11-2006, 03:34 AM   #4
T.Hsu
Member
 
Registered: Jan 2005
Posts: 178

Rep: Reputation: 31
The problem is printf, use echo instead or printf %b "$(hwclock)" "\n"
 
Old 01-11-2006, 03:41 AM   #5
Infernal211283
Member
 
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157

Original Poster
Rep: Reputation: 30
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.
 
Old 01-11-2006, 04:04 AM   #6
Infernal211283
Member
 
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157

Original Poster
Rep: Reputation: 30
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
 
Old 01-11-2006, 04:17 AM   #7
Infernal211283
Member
 
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157

Original Poster
Rep: Reputation: 30
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.
 
Old 01-11-2006, 04:19 AM   #8
T.Hsu
Member
 
Registered: Jan 2005
Posts: 178

Rep: Reputation: 31
double quotes

touch "$TIME"
 
Old 01-11-2006, 04:35 AM   #9
Infernal211283
Member
 
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157

Original Poster
Rep: Reputation: 30
Talking

^ thanks..
 
Old 01-11-2006, 04:37 AM   #10
T.Hsu
Member
 
Registered: Jan 2005
Posts: 178

Rep: Reputation: 31


Glad it helps. You may also try "touch -- $TIME" to see the results
 
Old 01-11-2006, 04:52 AM   #11
Infernal211283
Member
 
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157

Original Poster
Rep: Reputation: 30
Thumbs up

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
 
Old 01-11-2006, 05:26 AM   #12
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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,
 
Old 01-11-2006, 05:54 AM   #13
Infernal211283
Member
 
Registered: Nov 2004
Location: Mid. East / Israel
Distribution: Slackware 10.2, Gentoo
Posts: 157

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


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
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
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM
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 08:35 PM.

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