Solved

BASH: Shell script check without actually running it?

Posted on 2013-05-16
9
558 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
  • 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
 
LVL 11

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 500 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 11

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

708 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

13 Experts available now in Live!

Get 1:1 Help Now