LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-23-2022, 11:41 AM   #1
MienMatrix
LQ Newbie
 
Registered: Jun 2022
Posts: 6

Rep: Reputation: 0
Question Help Bash script for HISTTIMEFORMAT


Hello World,
i'm developing bash script to insert HISTTIMEFORMAT into /etc/profile to activate date and time in history file for all users linux starting from a server to all server of DataCenter.
I done some proofs but statemente echo send error: i put script follow:


Code:
#!/bin/bash
> /tmp/histDateTime.sh
i=/tmp/server_list.txt
c="cp /etc/profile /etc/profile_bck"
d="echo 'export HISTTIMEFORMAT="%d/%m/%y %T"' >> /etc/profile"
cat $i | while read A
do

cat <<EOF >> /tmp/histDateTime.sh
ssh $A "$c" ;"$d" 

EOF
done
sh /tmp/histDateTime.sh

I think also use some variable to separate data as:
Code:
#!/bin/bash
> /tmp/histDateTime.sh
i="/home/user/scripts/asset.csv"
c="cp /etc/profile /etc/profile_bck"
d=('export HISTTIMEFORMAT="%d/%m/%y %T"')
e="/etc/profile"

cat $i | while read A
do
ssh $A "$c" ; "$d" >> "$e"
but this goes error too :_(

I'm sure to not be master in bash script, so please if you can give me help i thank you forever.

Best regards
Enrico

Last edited by MienMatrix; 06-24-2022 at 07:38 AM.
 
Old 06-23-2022, 12:00 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Code:
#!/bin/bash
echo "cp /etc/profile /etc/profile_bck" > /tmp/script
echo "echo 'export HISTTIMEFORMAT=\"%d/%m/%y %T\"' >> /etc/profile" >> /tmp/script

while read A
do
    cat /tmp/script | ssh $A /bin/bash
done < /tmp/server_list.txt
something like this may work (not tested)
 
Old 06-23-2022, 12:12 PM   #3
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by MienMatrix View Post
d="echo 'export HISTTIMEFORMAT="%d/%m/%y %T"' >> /etc/profile"
try
Code:
d="echo -e 'export HISTTIMEFORMAT=\"%d/%m/%y %T\"' >> /etc/profile"
 
Old 06-23-2022, 01:24 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
It might help knowing what distribution is running on the servers.

I would create a file that contains your export command and then transfer it to the server's /etc/profile.d directory versus appending the command to the /etc/profile file.

You do have to log into the server as root or run a sudo command if your remote user has sudo privileges but it gets a little tricky if you need to be able to enter your password.

You also need a space following the %T i.e
Code:
export HISTTIMEFORMAT="%d/%m/%y %T "

Last edited by michaelk; 06-23-2022 at 06:22 PM.
 
Old 06-24-2022, 05:01 AM   #5
MienMatrix
LQ Newbie
 
Registered: Jun 2022
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
Code:
#!/bin/bash
echo "cp /etc/profile /etc/profile_bck" > /tmp/script
echo "echo 'export HISTTIMEFORMAT=\"%d/%m/%y %T\"' >> /etc/profile" >> /tmp/script

while read A
do
    cat /tmp/script | ssh $A /bin/bash
done < /tmp/server_list.txt
something like this may work (not tested)
Thanks Pan64 for replay, i try it and then reply.. Stay tuned XD

edit: "A" is A=/tmp/server_list.txt right?

Last edited by MienMatrix; 06-24-2022 at 06:33 AM.
 
Old 06-24-2022, 05:11 AM   #6
MienMatrix
LQ Newbie
 
Registered: Jun 2022
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
It might help knowing what distribution is running on the servers.

I would create a file that contains your export command and then transfer it to the server's /etc/profile.d directory versus appending the command to the /etc/profile file.

You do have to log into the server as root or run a sudo command if your remote user has sudo privileges but it gets a little tricky if you need to be able to enter your password.

You also need a space following the %T i.e
Code:
export HISTTIMEFORMAT="%d/%m/%y %T "
Thanks for replay,
i explain my situation:
Operation system for server data center is RedHat version (about 600 servers more or less)
I'd like use a bridge server that has direct access to all servers of data center as root (sure), list server in file txt, with protocoll ssh. Right, i need this script to insert HISTTIMEFORMAT string into /etc/profile (after made a copy) to activate date and time in all users's history file.

I'll try your code also, thank you

let me check all solution
see you after
Enrico

Last edited by MienMatrix; 06-24-2022 at 05:14 AM.
 
Old 06-24-2022, 06:38 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Then as suggested I would copy the command as a file to the /etc/profile.d directory instead of appending to /etc/profile. /etc/profile runs the scripts in /etc/profile.d

Per Redhat
Quote:
First of all, the files in the /etc directory are global for all users, but should really avoid changing them if at all possible. If need to modify the global settings, you can use /etc/profile.d (this is to prevent your changes from getting wiped out when packages are upgraded).
 
Old 06-24-2022, 07:02 AM   #8
MienMatrix
LQ Newbie
 
Registered: Jun 2022
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
Then as suggested I would copy the command as a file to the /etc/profile.d directory instead of appending to /etc/profile. /etc/profile runs the scripts in /etc/profile.d

Per Redhat
Ok
So, if I understand correctly: if I insert in the path /etc/profile.d a file.sh containing
export HISTTIMEFORMAT = "% d /% m /% y% T"
export HISTTIMEFORMAT
into
/etc/profile.d/history.sh

history.sh would run automatically?
This it will be too much simple, because can create file history.sh and with scp script send in all server, right?

Thanks a lot
Regard
Enrico
 
Old 06-24-2022, 07:05 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by MienMatrix View Post
Ok
So, if I understand correctly: if I insert in the path /etc/profile.d a file.sh containing
export HISTTIMEFORMAT = "% d /% m /% y% T"
export HISTTIMEFORMAT
into
/etc/profile.d/history.sh

history.sh would run automatically?
This it will be too much simple, because can create file history.sh and with scp script send in all server, right?

Thanks a lot
Regard
Enrico
yes, that will be "automagically" executed together with /etc/profile
But it is syntactically incorrect, space is not allowed after/before = (equal sign).
 
Old 06-24-2022, 07:28 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
You only need the one line.
Code:
export HISTTIMEFORMAT="%d/%m/%y %T "
An example of the history output with time is:
Quote:
989 06/24/22 09:31:37 top
The space after the %T separates time from the actual command ran.
 
Old 06-24-2022, 07:38 AM   #11
MienMatrix
LQ Newbie
 
Registered: Jun 2022
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up [SOLVED] - Help Bash script for HISTTIMEFORMAT

Quote:
Originally Posted by pan64 View Post
yes, that will be "automagically" executed together with /etc/profile
But it is syntactically incorrect, space is not allowed after/before = (equal sign).
IT WORKS, GREAT!
spaces there aren't in command, maybe is bug of quote.

Thank you very much to solve my issue and thank to all.

BEST REGARD
Enrico
 
  


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: HISTTIMEFORMAT ignored by root user AnarKEY Linux - Software 4 05-26-2011 02:59 AM
-bash: HISTTIMEFORMAT: readonly variable chavete88 Slackware 2 11-11-2010 09:09 AM
Getting HISTTIMEFORMAT to work after logout benmie Linux - Newbie 9 10-14-2009 02:47 PM
HISTTIMEFORMAT in script problems. ncsuapex Programming 2 06-26-2008 02:10 PM
history with time stamps not working, HISTTIMEFORMAT mohammednv Linux - General 1 02-02-2008 08:06 AM

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

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