Solved

BASH: Shell script check without actually running it?

Posted on 2013-05-16
9
561 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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…
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.

776 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