LinuxQuestions.org
Review your favorite Linux distribution.
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 12-14-2009, 11:21 PM   #16
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705

Quote:
Originally Posted by ghostdog74 View Post
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.
You gave that script to someone else to use and you weren't even aware of the potential problems. Later you solved a potential problem (that you had not been aware of) that you never would have encountered if you had not used ls in the first place.

Quote:
What's the next unsafe scenario, if you may?
Ok, one more just for fun: spaces in file names:
Code:
touch a.txt
touch "c b.txt"
for f in $(ls *.txt) ; do echo "File is $f" ; done
for f in * ; do echo "File is $f" ; done

If you don't want to take my advice, that's fine.
Continue using ls.

Cheers,

Evo2.

PS. If you really want to discuss using ls in this fashion login to #linux at irc.freenode.net when GNU\Colossus is online: he'll have plenty to say on the topic.

Last edited by evo2; 12-14-2009 at 11:28 PM. Reason: PS.
 
Old 12-14-2009, 11:40 PM   #17
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by evo2 View Post
You gave that script to someone else to use and you weren't even aware of the potential problems. Later you solved a potential problem (that you had not been aware of) that you never would have encountered if you had not used ls in the first place.
i didn't say i will give my script to someone else to use. Even if i did, the problem is already taken care of.

Quote:
Ok, one more just for fun: spaces in file names:
Code:
touch a.txt
touch "c b.txt"
for f in $(ls *.txt) ; do echo "File is $f" ; done
for f in * ; do echo "File is $f" ; done
i always advocate that if you want to parse file names, don't use for loop with ls like that, since its useless use of ls. Use shell expansion. If you really want to do that, change IFS
Code:
IFS=$'\n';for i in $(ls *); do echo "$i"; done
or use a while read loop.

however, this example doesn't apply here in this problem. I am asking, what other "unsafe" scenario for this case only. I am parsing ls -l for the owner and filename like OP wanted , and i have already taken care of that. What else?
 
Old 12-14-2009, 11:47 PM   #18
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by ghostdog74 View Post
i didn't say i will give my script to someone else to use. Even if i did, the problem is already taken care of.
That is exactly what you did right here. You gave the script to the OP and only later fixed.

Anyway, I'm getting sick of this. As I said, you are welcome to ignore my advice.

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

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by evo2 View Post
That is exactly what you did right here. You gave the script to the OP and only later fixed.

Anyway, I'm getting sick of this. As I said, you are welcome to ignore my advice.

Evo2.
conclusion: your script is only "unsafe" if you don't know what you are doing. Good bye
 
Old 12-15-2009, 12:38 AM   #20
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
What's the next unsafe scenario, if you may?
File names containing non-printable characters including backspace, linend etc. More fully explained in Greg's WIKI in Why you shouldn't parse the output of ls.

EDIT: similar discussion beginning with this LQ post.

Last edited by catkin; 12-15-2009 at 01:38 AM. Reason: Formatting
 
1 members found this post helpful.
Old 12-15-2009, 01:22 AM   #21
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
yes, thank you. That's one point to keep in mind.
To get back to topic, I only want to know what's "unsafe" for OP's problem. If i may reiterate, he just wants to get .bash_history (which no newlines or special characters in file name) and parsing ls to get the owner ( and not filename ) is alright and not "unsafe". If i do want to parse file names as well and i know it possible that my filenames can contain other chars or newlines, I won't recommend parsing ls either.
 
Old 12-15-2009, 01:52 AM   #22
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghostdog74 View Post
I only want to know what's "unsafe" for OP's problem.
As you point out, .bash_history does not contain any unprintable characters and I do not know of any versions of ls that do not ordinarily put the user name in space-separated field 3 so your code is safe in all known circumstances -- except ls could be (and often is) an alias
Code:
c:/tmp$ alias ls='ls -Di'
c:/tmp$ ls -l
  total 8
  212610 srwxr-xr-x 1 c users    0 Dec 15 11:37 OSL_PIPE_1000_SingleOfficeIPC_f24b151f1666ce0a2d65526a3964194
  210604 drwx------ 2 c users 4096 Dec 15 11:35 ssh-CciYjZ3847
  212654 drwxr-xr-x 2 c users 4096 Dec 15 11:53 sv5pd.tmp
//DIRED// 58 119 168 182 231 240
//DIRED-OPTIONS// --quoting-style=literal
c:/tmp$ \ls -l
total 8
srwxr-xr-x 1 c users    0 Dec 15 11:37 OSL_PIPE_1000_SingleOfficeIPC_f24b151f1666ce0a2d65526a3964194
drwx------ 2 c users 4096 Dec 15 11:35 ssh-CciYjZ3847
drwxr-xr-x 2 c users 4096 Dec 15 11:53 sv5pd.tmp
 
Old 12-15-2009, 04:54 AM   #23
firehak
LQ Newbie
 
Registered: Dec 2009
Location: North Canton, OH
Distribution: Fedora
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks to everyone who replied. This was my final result and it works perfectly and now I can use it for class today.

Code:
#!/bin/bash

echo "Please note that if you do not have sudo priviliges, you only copy your own .bash_history."
echo "Please enter your user name: "
read admin

mkdir -pv /home/$admin/history

for f in /home/*/.bash_history ; do
  owner=$(stat --print %U $f)
  cp -p $f /home/$admin/history/${owner}.history && chown $admin /home/$admin/history/*
done
 
Old 12-15-2009, 05:02 AM   #24
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by firehak View Post
Thanks to everyone who replied. This was my final result and it works perfectly and now I can use it for class today.
Cool.

Just a couple of things. I'd probably put 'set -x' on the first line so that the script will exit if there are any errors. I'd also either move
the
Code:
chown $admin /home/$admin/history/*
to be just after the loop, or the other option is change it to

Code:
chown $admin /home/$admin/history/${owner}.history
The other thing I'm a bit confused about is that the user running the script, enters their username... the shell can get that from the $USER variable.. and that user will own the cp of the file anyway.

Cheers,

Evo2.

Last edited by evo2; 12-15-2009 at 05:04 AM.
 
Old 12-15-2009, 05:15 AM   #25
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by firehak View Post
Thanks to everyone who replied. This was my final result and it works perfectly
for a more efficient approach using stat, see post #6.
 
Old 12-15-2009, 06:38 AM   #26
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
Cool.

Just a couple of things. I'd probably put 'set -x' on the first line so that the script will exit if there are any errors. I'd also either move
the
Code:
chown $admin /home/$admin/history/*
to be just after the loop, or the other option is change it to

Code:
chown $admin /home/$admin/history/${owner}.history
The other thing I'm a bit confused about is that the user running the script, enters their username... the shell can get that from the $USER variable.. and that user will own the cp of the file anyway.

Cheers,

Evo2.
I'll try that. I used $USER at first, but when running sudo it took user root as the $USER. And for some reason running without sudo at school lets me see permission denied for every user, but running with sudo only copied 1 user, administrator, my teacher, and root.

EDIT:
And I still can't copy everyone. I get 2 people I'm actually supposed to copy, then administrator and root.

EDIT..again..
Sorry for the confusion. It did work, my teacher just told me he accidentally copied the same .bash_history to everyones folder.

Last edited by firehak; 12-15-2009 at 06:55 AM.
 
Old 12-16-2009, 09:33 AM   #27
firehak
LQ Newbie
 
Registered: Dec 2009
Location: North Canton, OH
Distribution: Fedora
Posts: 13

Original Poster
Rep: Reputation: 0
Hey, if it helps anyone, I was bored in class and modified it further. It now combines all the history files into one large file, with "username"'s history above their history section and I added the lastlog command to the end and saved results to a file called login_info

Code:
#!/bin/bash

echo "Please note that if you do not have sudo priviliges, you only copy
your own .bash_history."
echo "Please enter your user name: "
read admin

mkdir -pv /home/$admin/history
rm -Rf /home/$admin/history/*


for f in /home/*/.bash_history ; do
  owner=$(stat --print %U $f)
  cp -p $f /home/$admin/history/${owner}.history
  sed -i "1i-----------------------------------\n"${owner}"'s history\n-----------------------------------\n" /home/$admin/history/${owner}.history
  chown $admin /home/$admin/history/*
done

lastlog >> /home/$admin/history/login_info

cat /home/$admin/history/*.history >> /home/$admin/history/history
rm /home/$admin/history/*.history
 
Old 12-16-2009, 05:48 PM   #28
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
FYI: priviliges => privileges

 
  


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

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

All times are GMT -5. The time now is 04:20 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