LinuxQuestions.org
Help answer threads with 0 replies.
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 12-14-2009, 09:27 PM   #1
firehak
LQ Newbie
 
Registered: Dec 2009
Location: North Canton, OH
Distribution: Fedora
Posts: 13

Rep: Reputation: 0
bash scripting question


I'm trying to copy the .bash_history file out of each users home directory and paste it into a directory in my teachers home folder. Is there any way I could can return just the owner of a file? If I could get just that output I know I could easily write the rest of the script on my own.
 
Old 12-14-2009, 09:32 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
set -- $(ls -l /path/to/bash_history)
echo "Owner is: $3"
 
1 members found this post helpful.
Old 12-14-2009, 09:42 PM   #3
firehak
LQ Newbie
 
Registered: Dec 2009
Location: North Canton, OH
Distribution: Fedora
Posts: 13

Original Poster
Rep: Reputation: 0
Thank you so much, I've been looking around and trying various ways all day.

EDIT:
Hm. I still can't get it right. Here's my script:

Code:
#! /bin/bash

set -- $(ls -l /home/*/.bash_history)
cp /home/*/.bash_history /home/stephan/$3.history
And here is my output:

Quote:
cp: target `/home/stephan/lfs.history' is not a directory
So I apparently need more help than I thought. I'm assuming it's also only grabbing the first value found by the wildcard, but I'm trying to grab every users history. I have a linux from scratch user and my user here, but at school there is about 20 on the computer and he want's to make sure the juniors are actually doing what they're supposed to.

Last edited by firehak; 12-14-2009 at 10:01 PM.
 
Old 12-14-2009, 10:40 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
In practice it is better not to parse the output of ls. I this case you can uses the stat command.
You can also use a loop, copy the files one, by one. The following should work, but is *untested*

Code:
#!/bin/bash
for f in /home/*/.bash_history ; do
  owner=$(stat --print %U $f)
  cp $f /home/stephan/${owner}.history
done

Cheers,

Evo2.

Last edited by evo2; 12-14-2009 at 10:42 PM. Reason: wrong shell
 
1 members found this post helpful.
Old 12-14-2009, 10:46 PM   #5
firehak
LQ Newbie
 
Registered: Dec 2009
Location: North Canton, OH
Distribution: Fedora
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by evo2 View Post
In practice it is better not to parse the output of ls. I this case you can uses the stat command.
You can also use a loop, copy the files one, by one. The following should work, but is *untested*

Code:
#!/bin/bash
for f in /home/*/.bash_history ; do
  owner=$(stat --print %U $f)
  cp $f /home/stephan/${owner}.history
done

Cheers,

Evo2.
Thank you so much, I just tested and it worked perfectly. I just need to add a "chown" or "chmod" to make it viewable for the myself and the other 2 admins now. You saved me a lot of time =D
 
Old 12-14-2009, 10:52 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by firehak View Post
So I apparently need more help than I thought. I'm assuming it's also only grabbing the first value found by the wildcard, but I'm trying to grab every users history. I have a linux from scratch user and my user here, but at school there is about 20 on the computer and he want's to make sure the juniors are actually doing what they're supposed to.
that piece of code is for only 1 file. to do it for many
Code:
ls -l /home/*/.bash_history | while read -r  line
do 
   set -- $line
   echo "owner: $3, file: ${@:8}"
   # cp your stuff
done
similarly, if you want to use stat like this for one invocation.
Code:
stat -c "%U:%n" /home/*/.bash_history| while IFS=":" read -r owner filename
do
  cp "$filename" "/home/stephane/${owner}.history"
done
be careful of overwriting files with the same owner though.

lastly, for your experimentation
Code:
stat -c "cp %n /home/stephen/%U.history"

Last edited by ghostdog74; 12-14-2009 at 11:19 PM.
 
1 members found this post helpful.
Old 12-14-2009, 11:01 PM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by ghostdog74 View Post
that piece of code is for only 1 file. to do it for many
Code:
ls -l /home/*/.bash_history | while read -r  line
do 
   set -- $line
   echo "owner: $3, file: ${@:8}"
   # cp your stuff
done
Seriously, it is a bad idea to try to parse the output of "ls -l". One reason in this case is that the date format can be different depending on the users environment and as such the field number, in $line for the file name could change. ls should be used for human readable output, stat is a better option for parsing in scripts.

Evo2.
 
Old 12-14-2009, 11:18 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by evo2 View Post
Seriously, it is a bad idea to try to parse the output of "ls -l". One reason in this case is that the date format can be different depending on the users environment and as such the field number, in $line for the file name could change. ls should be used for human readable output, stat is a better option for parsing in scripts.

Evo2.
true, it good to use stat in general since stat has many more features, but not for this case and other simple cases. ls -l uses the same stat library as "stat" command. if i am worried about time format, there are various ways to go around it. I can get rid of the date and time portion.
Code:
ls -lg --time-style="+" |while read -r line
do
    set -- $line
    echo "owner: $3, filename: ${@:5}"
done
therefore, the notion of "bad idea to parse ls output" is not true for this case

anyway, i have also provided stat suggestion so its up to OP

Last edited by ghostdog74; 12-14-2009 at 11:29 PM.
 
Old 12-14-2009, 11:19 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by evo2
Code:
#!/bin/bash
for f in /home/*/.bash_history ; do
  owner=$(stat --print %U $f)
  cp $f /home/stephan/${owner}.history
done
why not let stat do the file parsing instead of calling stat in the for loop for each file?

Last edited by ghostdog74; 12-14-2009 at 11:25 PM.
 
Old 12-14-2009, 11:30 PM   #10
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
further, if i am worried about time format, there are various ways to go around it. I can get rid of the date and time portion.
Sure, you can pass all sorts of options to ls, but the point is that it is both safer and easier, to not use it at all.

Quote:
anyway, i have also provided stat suggestion so its up to OP
Yeah, you seemed to be editing your post while I was writing mine.

Evo2.
 
Old 12-14-2009, 11:38 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by evo2 View Post
Sure, you can pass all sorts of options to ls, but the point is that it is both safer and easier, to not use it at all.
if this principle applies to you, that's fine. But not all adheres to that notion.

Last edited by ghostdog74; 12-14-2009 at 11:39 PM.
 
Old 12-14-2009, 11:45 PM   #12
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by ghostdog74 View Post
if this principle applies to you, that's fine. But not all adheres to that notion.
Sure, safety and ease of use are not what everyone is looking for.

Bye,

Evo2.
 
Old 12-14-2009, 11:49 PM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by evo2 View Post
Sure, safety and ease of use are not what everyone is
.
well, i am curious. What's so "unsafe" about parsing ls -l output, in THIS case, since I have shown how to get owner and filename correctly. mind giving an example?

Also "ease of use" is subjective, so i am not talking about it.
 
Old 12-15-2009, 12:00 AM   #14
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by ghostdog74 View Post
well, i am curious. What's so "unsafe" about parsing ls -l output, in THIS case, since I have shown how to get owner and filename correctly. mind giving an example?
I did. I told you about the time format issue. You then modified your script so that the date of the file was not not printed. This is exactly the sort or problem you will get when trying to parse the output of ls in scripts.

Quote:
Also "ease of use" is subjective, so i am not talking about it.
Fine by by me.

Evo2.
 
Old 12-15-2009, 12:05 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by evo2 View Post
I did. I told you about the time format issue. You then modified your script so that the date of the file was not not printed. This is exactly the sort or problem you will get when trying to parse the output of ls in scripts.
If i know my time format and parse according to that, its not a problem. If i am going to run my script in various linux machines with different time format, then i can get rid of the timestamp and it will work as well... ok, so one aspect solved. What's the next unsafe scenario, if you may?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash scripting question methodtwo Linux - Newbie 2 02-16-2009 01:13 AM
Bash Scripting: Question About tr tvynr Linux - Software 16 09-01-2006 08:25 AM
Bash scripting question ajaye1971 Linux - Newbie 1 11-16-2005 08:29 PM
BASH Scripting question flagg0204 Programming 4 12-23-2004 08:59 AM
Bash scripting question Hammett Linux - General 4 11-29-2004 07:29 AM

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

All times are GMT -5. The time now is 08:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration