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

Posted on 2006-05-08
Last Modified: 2013-12-06

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
Question by:JoelRidley
    LVL 48

    Assisted Solution

    Simplistically, you could do

    read command
    if [ -x "$command" ]
        echo "$command is executable, so is probably OK to run from cron"
        echo "$command is not found or not executable"
    LVL 23

    Accepted Solution

    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...

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

    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


    Author Comment

    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

    Author Comment

    PS ( cd  /var;  PATH='';   text -x $command )
    should be ( cd  /var;  PATH='';   test -x $command )
    I believe Joel
    LVL 23

    Expert Comment

    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


    /usr/bin/logger -t  crontabcmd   -p   "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


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
    1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now