[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Linux script to test if users command is valid and return error or no error



Hello Expert,
I am writing a crontab script that will ask the user to input the desired command to be run by crontab. Is there a way I can pass the command to the command line to test to see if it is a valid command and if not then get an error back that I can then tell the user that the command is invalid and they need to try again? The command must be valid for crontab to complete successfully. I need to do this from within my script. This script will run on Fedora Core 5 Thank you, Joel
0
JoelRidley
Asked:
JoelRidley
  • 2
  • 2
2 Solutions
 
TintinCommented:
Simplistically, you could do

read command
if [ -x "$command" ]
then
    echo "$command is executable, so is probably OK to run from cron"
else
    echo "$command is not found or not executable"
fi
0
 
MysidiaCommented:
Unfortunately,   using  -x   could find a file in the current directory -- which could then fail when
crontab tries to find the file.

You could try running the command and checking for successful return status...

$command
if  test $? -eq 0 ; then
  ## command completed successfully
fi

However, there is a good chance the user might not want this -- if the command has a side-effect, for instance,
there would be a spurious run of the command, before the planned time.


I'd just check if the file exists and is executable,   switching directories to ensure the user
has to enter the full path for the file...

( cd  /var;  PATH='';   text -x $command )
if  test $?  -ne 0 ; then
      echo "$command is not found or not executable"
      exit 1
fi




0
 
JoelRidleyAuthor Commented:
Hello Mysidia,
I tried your suggestions and the last one gives me a + stopped[10] which is exactly what I needed, Is there a way I can write the UID and the command to a file for reviewing later if the command failed? as it is the script stops. Thanks joel P.S. I am posting a directory/file question also TinTin your suggestion works also but Mysidia is what I was looking for 50 poiunts though for your answer
0
 
JoelRidleyAuthor Commented:
PS ( cd  /var;  PATH='';   text -x $command )
should be ( cd  /var;  PATH='';   test -x $command )
I believe Joel
0
 
MysidiaCommented:
Yes...    test -x

You can write the  UID to a file, but  the user would need to be able to write the file; that would
mean the file is world-writable if the script can change add to it no matter who runs it..


The simplest method is just to use the logger tool

I.E.

/usr/bin/logger -t  crontabcmd   -p user.info   "User  ${UID}(euid=${EUID})... bad command  $command"


Which  will send the message to the system logging daemon as a   user message of priority level info.

Where and how it is logged is configured in /etc/syslog.conf
but generally, it will go to  /var/log/messages


0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now