Solved

BASH: Shell script check without actually running it?

Posted on 2013-05-16
9
559 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 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

914 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