Bashrc File - Conditional Command Execution?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bashrc File - Conditional Command Execution?
# 1  
Old 02-10-2015
Bashrc File - Conditional Command Execution?

Hello All,

I was wondering if there is a way to execute a command in my ".bashrc" file based on how I logged into the PC?

I was thinking maybe there is a way to check how the user (*myself) logged in, maybe somehow with the who command along with something else, but I'm not sure... I know I could run who and then check to see if there is a remote user logged in, but I didn't see a way of proving that the remote user shown in the who command's output was me or not?

I know I could do this below, but there could also be other user's remotely logged in, or multiple terminals open, etc... So I couldn't just say something like, "If there is an IP Address found in the 'who' commands output, then I'm a remote user...."
Code:
# who
user1 :0           2014-07-08 00:00 (console)
user1 pts/0        2015-02-10 10:47 (:0.0)
user2 pts/1        2015-02-10 12:01 (192.168.123.789)
user1 pts/2        2015-02-10 10:55 (:0.0)
user1 pts/3        2015-02-10 10:56 (:0.0)
user2 pts/4        2015-02-10 12:28 (192.168.123.456)      <--**** THIS IS ME ****

So I guess, is there anyway to tell that user2 in the output above is myself? Maybe by finding out which pts I'm using..?

Could anyone point me in the right direction on where I should/could start with finding this out..? Any thoughts or suggestions would be greatly appreciated!

Thanks in Advance,
Matt



********** EDIT **********
I think I may have found something in Google'ing around a bit more...
I knew of the "who", "whoami", "w", "users" commands but I had never seen the "who am i" command (*with Spaces) which printed the following:
Code:
# who am i
user2     pts/4        2015-02-10 12:28  (192.168.123.456)

So I guess I'll see if I can give this a try...

Last edited by mrm5102; 02-10-2015 at 01:56 PM..
# 2  
Old 02-10-2015
This may work if your host has a fixed IP address assigned to it. With DHCP this may change over time. Anyhow, having several users log into the same account is usually deprecated for its inherent insecurity.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-10-2015
Hey RudiC, thanks for the reply.

Yea, I ruled out using the IP Address because of that reason (*DHCP) and also that other users could be logged in remotely as well... Not sure what version that has been deprecated on but the remote PC is running OpenSuSE 13.1 Kernel 3.14... so its fairly newer I suppose...

But anyway, I think the "who am i" command should help me do what I want. I was thinking of something like this below. This is just a simple test, but it seems to have worked:
Code:
REGEX="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

if [[ `who am i` =~ $REGEX ]]
 then
    echo "TRUE"
else
    echo "FALSE"
fi

After SSH'ing in I ran this file and it printed "TRUE", then I physically went to this machine and ran this file and it printed "FALSE". So I think it should work.

And I know the REGEX isn't very precise in terms of fitting into what the standards of what an IP Address can be (*i.e. the regex allows numbers higher then 255) but for my purposes I think this should suffice...

Thanks Again for the reply,
Matt
# 4  
Old 02-10-2015
If you use sudo, try the "id" command. That might give you additional information.
# 5  
Old 02-10-2015
Quote:
Originally Posted by mrm5102
Hey RudiC, thanks for the reply.

Yea, I ruled out using the IP Address because of that reason (*DHCP) and also that other users could be logged in remotely as well... Not sure what version that has been deprecated on but the remote PC is running OpenSuSE 13.1 Kernel 3.14... so its fairly newer I suppose...
Not "depreciated" as in "no longer possible", but "depreciated" as in "giving several different people access to the same account is now widely regarded as dumb and unnecessary".
# 6  
Old 02-10-2015
Oh ok, gotcha... Its just a server/PC that has one user and only 2 of use even know the login so it works for our purposes, thanks.

I did see the "id" command while searching around but it didn't show whether or not the user logged-in remotely or not, but the "who am i" one did... And on a funny note I also came across "who mom likes" which I thought was pretty funny, lol....

By anyway, thanks again for the replies. Much appreciated!

Thanks,
Matt
# 7  
Old 02-12-2015
Quote:
Originally Posted by mrm5102
Oh ok, gotcha... Its just a server/PC that has one user and only 2 of use even know the login so it works for our purposes, thanks.
That's how it often begins, and then suddenly you need to add more users... Before you know it, there's 12 people on one login with no way to know who did what.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional execution

Here's an interesting (to me, anyway) little puzzle. Background: I have a process that restores a number of large(ish) files from tape backup. As an individual file is being written, it is owned by root, then the ownership is changed when that file is complete. Since this process can take... (3 Replies)
Discussion started by: edstevens
3 Replies

2. Shell Programming and Scripting

Ed command execution in script file

for file in File_Name* do ed -s $file << 'EOF' 0a $Header_Record . $a . w EOF done The above command is executing in the unix box but is not... (2 Replies)
Discussion started by: Kanch
2 Replies

3. Shell Programming and Scripting

Conditional Execution of a Script.

I have a unix shell script Test.sh more Test.sh echo "Calling dbquery1.sh...." ./dbquery1.sh echo "Calling dbquery2.sh...." ./dbquery2.sh more dbquery1.sh sqlplus -s user1/password1@DB_SID @/tmp/storedprocedures/Hello.rcp I run Test.sh However, I do not want dbquery2.sh to be... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

Conditional grep command to search entire file

Let me give you a complete example what I am trying to achieve. 1. Below is the log file structure where I need 2,5 and 14th column of the logs after grepping through the linkId=1ddoic. Log file structure:- abc.com 20120829001415 127.0.0.1 app none11111 sas 0 0 N clk Mozilla/5.0... (3 Replies)
Discussion started by: kmajumder
3 Replies

5. Shell Programming and Scripting

search and conditional execution

Hi, I have a list of files with different filenames like nam0001.txt,pan0001.txt etc coming in /data/inbox from various places. I needed a shell script which would:- 1)searches for a word from the filenames (coming in inbox directory) provided in input parameter. 2)if found, put in... (2 Replies)
Discussion started by: Saiesh
2 Replies

6. UNIX for Dummies Questions & Answers

Conditional execution of statements

Hi , I have a script which has multiple awk and sed commands like below. First one :- find /root/src/ -name "*csv" -size 0 -exec rm {} \; Second one: - ls *SRE*.txt > SRT_TRAN.dat rm *asr*.txt Third one :- find /root/src/ -name '*.csv' | while read FILENAME ; do awk... (2 Replies)
Discussion started by: shruthidwh
2 Replies

7. Shell Programming and Scripting

Conditional execution

Hi All, I want to echo a message in case a system is reachable by ping or echo a different message in case it's not reachable. Sample code i wrote is ping localhost -n 2 | grep 'ttl' > ping_op; ls ping_op > /dev/null && drReachable=Alive; echo -e `date`: \\t "DR server is reachable" >>... (5 Replies)
Discussion started by: Mr. Zer0
5 Replies

8. Shell Programming and Scripting

Help on shell script conditional execution when CPU Idle > 60%

I need a shell script that will monitor a few conditions and not execute until the these conditions are met. The problem I'm having is that I can not perform a database snapshot (backup) of a sybaseIQ database unless the CPU Status Idle % is above 60% or the snapshot (backup) fails. If... (2 Replies)
Discussion started by: pancona99
2 Replies

9. Shell Programming and Scripting

unix script for conditional execution

Below is my shell script. I am trying to execute two different BTEQ scripts depending on the day of the week. So on a saturday I will execute a certain BTEQ script and on other weekdays I will run the other script. #!/bin/ksh dt=`date +"%a"` if then bteq > final_output <<- EOF .run... (3 Replies)
Discussion started by: Mihirjani
3 Replies

10. Shell Programming and Scripting

Conditional execution and parallel jobs

how can i process jobs parallel with conditions below. Script1.ksh Script2.ksh Script3.ksh Script4.ksh Script5.ksh Script6.ksh Script7.ksh Script8.ksh Script9.ksh Script10.ksh After successful completion of Script1.ksh I need to run Script7.ksh. After successful... (4 Replies)
Discussion started by: ford2020
4 Replies
Login or Register to Ask a Question