Solved

BASH: Shell script check without actually running it?

Posted on 2013-05-16
9
565 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Log File Creation with Header and Footer 17 130
curl SSL certificate unable to get local issuer certificate 15 106
open source backup solution 1 36
awk variable in printf 1 34
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…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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.:
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…

830 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