?
Solved

BASH: Shell script check without actually running it?

Posted on 2013-05-16
9
Medium Priority
?
576 Views
Last Modified: 2013-05-18
Hi,

Is there a way to check in bash that an actual shell script can be executed without actually running it?

Thanks!
0
Comment
Question by:g0all
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39173845
test -x file

True if file exists and is executable.  True indicates only that the execute
flag is on.  If file is a directory, true indicates that file can be searched.
0
 
LVL 13

Expert Comment

by:stergium
ID: 39173849
Hello.
an ls -l would show you if it is executable without running it.
Do you want to check the syntax?
0
 
LVL 1

Author Comment

by:g0all
ID: 39173851
Syntax also, yes. I'm not interested in checking permissions.
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 12

Expert Comment

by:tel2
ID: 39173885
Does this work for you, g0all?
    bash -n scriptname
or maybe:
    bash -n ./scriptname

I understand some people use that, but it doesn't seem to work on my Linux flavour (CentOS).
0
 
LVL 1

Author Comment

by:g0all
ID: 39173890
It doesn't work for me neither. It shows no output.

test -x shows no output also.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39173918
test does not show output, it returns an exit status
     0       expression evaluated to true.

     1       expression evaluated to false or expression was missing.

     >1      An error occurred.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 39173944
bash -n

checks syntax but does not execute commands.

So if your syntax is correct it's quite normal that there is no ouput.

Try

bash -xvn ...

Please note that checking of command availability (PATH, spelling) or correctness of the command invocation (flags, parameters) cannot be checked this way (nor any other shell-based way I'm aware of).
0
 
LVL 19

Expert Comment

by:jools
ID: 39174615
I think you want to use ozo's response, use the test -x in an `if` context if it helps if you just check for 0 success you know the file can be executed.

eg/
ERR=`text -x filename`
if [[ ${ERR} -eq 0 ]]
then
   echo "success"
else
   echo "fail"
fi

Open in new window

0
 
LVL 12

Expert Comment

by:tel2
ID: 39176291
Hi jools,

That's not a syntax check though, is it?

> Do you want to check the syntax?
> Syntax also, yes. I'm not interested in checking permissions.

Also, you might like to test your solution, as it has at least 1 bug.
0

Featured Post

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

801 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