LinuxQuestions.org
Review your favorite Linux distribution.
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 10-05-2009, 11:20 AM   #1
ColInvictus
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 15
Test if a script has already been run


I've make some changes to my ".bashrc" and added a ".bash_aliases". I'd like to test in ".bash_aliases" if "/etc/bash_completion" has been run, so I know if it's worth adding completions for my aliases. Does anyone know if it's possible to check if a particular script has been executed recently - that is, since the bash session started? Or would I need to figure out a way of testing for the effects of running bash_completion?
 
Old 10-05-2009, 11:35 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by ColInvictus View Post
Does anyone know if it's possible to check if a particular script has been executed recently - that is, since the bash session started? Or would I need to figure out a way of testing for the effects of running bash_completion?
AFAIK, there is no way to determine whether a particular script has been executed since the bash session started which leaves you wanting to test for the effects of running bash_completion.
Code:
declare -f _complete >/dev/null
completion_active=$?
This will leave $completion_active set to 0 if completion is active and 1 otherwise.
 
Old 10-05-2009, 01:16 PM   #3
ColInvictus
Member
 
Registered: Apr 2009
Posts: 42

Original Poster
Rep: Reputation: 15
That's what I thought, but it's nice to have it confirmed before I go spending hours looking for a solution which doesn't exist, so thanks. And thanks for the test, too.
So there's no way to find the last time /any/ executable was run without said executable specifically writing that info to some persistent location?
 
Old 10-05-2009, 01:46 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by ColInvictus View Post
So there's no way to find the last time /any/ executable was run without said executable specifically writing that info to some persistent location?
AFAIK that is correct. The question often comes up when sysadmins suspect malfeasance and hope for some "Akashic records" or when they want to audit users' activity when they hope shell history can be used but shell history is a user convenience facility, not an audit facility and there are just too many ways around it for it to be an effective audit tool except for the most naive users -- who are not the dangerous malfeasants of interest. Even in the benign case of shell completion, bash history is useless. A few commands result in entries in /var/log files, but only a few and it is the effects of those commands rather than the commands themselves that are logged. If you are interested to know more, ask in the Security forum.
 
Old 10-05-2009, 02:15 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
On the other hand, it's fairly simple to have that script use the touch function to create a 0-byte file in some known location (e.g., ~/) and then to use the test function to compare the date/time of the file creation with the current date/time and base some action on the test result.

Often one such files a "flags" to tell other scripts what been started and whats been completed.

(By the way, in most bash implementations, test is - effectively - aliased to the " [ ... ] " inline function. "Effectively," because the inline function is most often a bash built-in function that works like the test function in /bin/test.)
 
Old 10-05-2009, 04:36 PM   #6
ColInvictus
Member
 
Registered: Apr 2009
Posts: 42

Original Poster
Rep: Reputation: 15
@catkin: thanks, i like the idea of using declare to test if something has already been set

@PTrenholme: isn't test a bash builtin too? is there a difference between the builtin and the /bin equivalent, and does bash preferentially use the builtin?
 
Old 10-05-2009, 06:09 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by ColInvictus View Post
. . .
@PTrenholme: isn't test a bash builtin too? is there a difference between the builtin and the /bin equivalent, and does bash preferentially use the builtin?
bash will use it's "built-in" version unless you explicitly use the /bin/test function call. The executable form, /bin/test, is provided to support non-bash programs that want to use a test function.

Look at info test for a detailed discussion of how to use the test functionality to compare things like the creation time of files. Also useful is the newer command which will tell you if one file entry is "newer" than another entry. (See man newer for details.)

Another suggestion: Use the script command to capture any output generated by a .bashrc (after it parses the last script output) and use that information to decide if you need to do your completion thing.
 
Old 10-05-2009, 10:03 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You could look at the atime on the file, but if it's in /etc, that usually means anyone on the system might have used it (unless its a desktop with only you on it).
 
  


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
if you run perl, could you test this script: 2007fld Programming 1 07-20-2007 06:21 PM
Is a script, run at boot time from init.d, run with root authority? tmbrwolf53 Linux - Server 2 03-31-2007 09:15 PM
How to test a run-time of a C program frankie_DJ Programming 2 10-07-2006 05:03 AM
KDE g++ program doesn't run test.cpp richikiki Programming 6 03-17-2006 03:47 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 10:34 AM


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