PDA

View Full Version : [SOLVED] Bash scripting variables



cortman
January 18th, 2012, 06:48 PM
I was wondering if I can take the output of a command such as


ls -l my_file | awk '{ print $6 }'

and assign it as a variable's value?
This would in effect set the variable as the last-modified date of my_file.
How would I do this? Or any other way to grab info about a file (its datestamp, read/write permissions, size, etc.) and assign it to a variable?

MG&TL
January 18th, 2012, 06:51 PM
myvar=$(ls -l my_file | awk '{ print $6 }')
echo $myvar

for instance. :)

|{urse
January 18th, 2012, 06:54 PM
Or you can just create a new file for that and use it as a variable.

touch file && ls -l my_file | awk '{ print $6 }' > file

aromo
January 18th, 2012, 07:53 PM
or use back quotes:
myvar=`ls -l my_file | awk '{ print $6 }'`
echo $myvar

nothingspecial
January 18th, 2012, 08:00 PM
or use back quotes:
myvar=`ls -l my_file | awk '{ print $6 }'`
echo $myvar

backticks are deprecated for command substitution. See

http://wiki.bash-hackers.org/scripting/obsolete

Lars Noodén
January 18th, 2012, 08:01 PM
or use back quotes

I learned using backticks, too, but nowadays that method is deprecated.

Here is a good explanation of why is $(...) is preferred over backticks:
http://mywiki.wooledge.org/BashFAQ/082

|{urse
January 18th, 2012, 08:13 PM
Yea, its too hard to discern the grave ` vs ' with a lot of fonts.

cortman
January 18th, 2012, 08:19 PM
myvar=$(ls -l my_file | awk '{ print $6 }')
echo $myvar

for instance. :)

This worked great. I can't believe I didn't think of it before, but oh well.
NOW my current problem is that I'd like to append the contents of the variable to a folder name. I used the mv command to


mv folder folder.$my_var

This worked great with a standard blank file, but will not work with a folder. Mv didn't have any options that would fix it. Am I missing something really basic?

MG&TL
January 18th, 2012, 08:49 PM
It works fine, here, but without the dot. error message?

cortman
January 18th, 2012, 09:00 PM
mv: target `2012-01-18' is not a directory


Here's my code:



mkdir /home/cortman/data/backup
cp /home/cortman/doug /home/cortman/data/backup
date=$(ls -l /home/cortman/data/backup | awk '{ print $6 }')
mv /home/cortman/data/backup /home/cortman/data/backup.$date

MG&TL
January 18th, 2012, 09:21 PM
mv: target `2012-01-18' is not a directory


Here's my code:



mkdir /home/cortman/data/backup
cp /home/cortman/doug /home/cortman/data/backup
date=$(ls -l /home/cortman/data/backup | awk '{ print $6 }')
mv /home/cortman/data/backup /home/cortman/data/backup.$date



mkdir /home/cortman/data/backup
cp /home/cortman/doug /home/cortman/data/backup
date=$(ls -l /home/cortman/data/backup | awk '{ print $6 }')
mkdir /home/cortman/backup.$date && mv /home/cortman/data/backup
/home/cortman/data/backup.$date


fixed, I think.

Vaphell
January 18th, 2012, 09:23 PM
i think he has a problem because ls -l <dir> shows dir's contents not its properties
ls -ld <dir> would do the trick

do normal ls -l and filter in awk step

ls -l | awk '/'"${selected}"'/{ print $6 }'
example:

$ selected=Music
$ ls -l | awk '/'"${selected}"'/{ print $6 }'
2011-11-10

or you can try a different approach and get the time using the stat command (it doesn't care about the object type)

$ stat -c '%y' "${selected}"
2011-11-10 07:23:39.796408979 +0100

cortman
January 18th, 2012, 09:27 PM
i think he has a problem because ls -l <dir> shows dir's contents not its properties
ls -ld <dir> would do the trick

do normal ls -l and filter in awk step

ls -l | awk '/'"${selected}"'/{ print $6 }'
example:

$ selected=Music
$ ls -l | awk '/'"${selected}"'/{ print $6 }'
2011-11-10

or you can try a different approach and get the time using the stat command (it doesn't care about the object type)

$ stat -c '%y' "${selected}"
2011-11-10 07:23:39.796408979 +0100

Aha! That's the problem, I can see now.

I'll try this and MG&TL's suggestion. Thanks to both of you!

cortman
January 18th, 2012, 09:31 PM
Changing "ls -l" to "ls -ld" did the trick.

Thanks a lot to all!!