Reputation: 134
there is .sh file in one of the directories for eg: path = /opt/WD/CD/SCD/temp.sh
is there any function is c++ to check whether file in that location(path) is a script(.sh) file or not
bool ValidCommand(const string & path)
{
bool return = true;
{
if (!access(path.c_str(),X_OK))
{
return = true;
}
else
return = false;
}
path = /opt/WD/CD/SCD/temp.sh
access is not working for me:(
Upvotes: 1
Views: 84
Reputation: 153899
Just how precise do you want it. It's fairly straightforward to read
the directory and look for filenames ending in .sh
.
(boost::filesystem
has everything you need for that.) But of course,
I could name my C++ headers file.sh
if I wanted (and was a bit
perverse). A considerably more accurate test would involve reading the
start of the file: if the first two bytes are "#!"
, it's probably a
script of some sort, and if the first line matches
"#!\\s*(?:/usr)?/bin/sh/[a-z]*sh"
, then you're almost sure it's a shell
script. Drop the [a-z]*
, and it's a Bourne or a Posix shell script.
Upvotes: 0
Reputation: 392833
Have a look at
Upvotes: 1
Reputation: 47493
You can use stat. If it failed (returned -1), the file doesn't exist.
Upvotes: 0
Reputation: 96233
Maybe stat
does what you need, otherwise you can look at the source for file
and see how it does it.
Upvotes: 0