?
Solved

check existence and attributes of multiple files on unix solaris

Posted on 2009-04-23
5
Medium Priority
?
825 Views
Last Modified: 2013-12-27
i have to parse , and ftp some files, the number of files is unknown but their names are
like *afc*, before starting to work, i want to check if these files exists, and their permisions on the unix system .
can someone help me ? it can be a c source file , or a shell script
i saw this solution,
#include  <stdio.h>
#include  <stdlib.h>
#include <unistd.h>

char *CONFIG_FILE = "user_config";

int main( void )
{
      /* Check for existence */
   if( (access(CONFIG_FILE, F_OK)) != -1 )
   {
      printf( "File exists\n" );

         /* Check for read permission */
      if( (access(CONFIG_FILE, R_OK)) != -1 )
         printf( "File has read permission\n" );

         /* Check for write permission */
         if( (access(CONFIG_FILE, W_OK)) != -1 )
             printf( "File has write permission\n" );
             
         /* Check for write permission */
            if( (access(CONFIG_FILE, X_OK)) != -1 )
                printf( "File has execute permission\n" );
     
   }
   else
   {
      printf( "File does not exists\n" );
   }
}
but i could not understand , where i should write the file names for this program to check, or does it work for multiple files

0
Comment
Question by:230409
  • 2
  • 2
5 Comments
 
LVL 3

Assisted Solution

by:sugarfreeless
sugarfreeless earned 600 total points
ID: 24220592
Here is a nice script from nixcraft.


#!/bin/bash
# Shell script to find out whether file has read, write and execute
# permission
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter file name : "
read file
 
# find out if file has write permission or not
[ -w $file ] && W="Write = yes" || W="Write = No"
 
# find out if file has excute permission or not
[ -x $file ] && X="Execute = yes" || X="Execute = No"
 
# find out if file has read permission or not
[ -r $file ] && R="Read = yes" || R="Read = No"
 
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"

Open in new window

0
 
LVL 40

Accepted Solution

by:
omarfarid earned 1400 total points
ID: 24221213
the script above can be customized to handle multiple files

for file in  *afc*
do
   # find out if file has write permission or not
   [ -w $file ] && W="Write = yes" || W="Write = No"
 
   # find out if file has excute permission or not
   [ -x $file ] && X="Execute = yes" || X="Execute = No"
 
   # find out if file has read permission or not
   [ -r $file ] && R="Read = yes" || R="Read = No"
 
   echo "$file permissions"
   echo "$W"
   echo "$R"
   echo "$X"
done
0
 

Author Comment

by:230409
ID: 24221684
is it ok to save these scripts as

script_name.sh and run,

can i use like bourne shell scripts or how can i execute at may system unix solaris 10

thanks
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 1400 total points
ID: 24222628
you save the script in a text file and name the way you like e.g. myscript
this script can run as sh or ksh or bash script
if you want to run it always with a particular shell then put line like the one below at the top

#!/usr/bin/ksh

to make the script executable run

chmod +x scriptname

to run it , use

./scriptname

if you want to call it from any where then move it to your bin dir and add your bin dir to PATH
0
 

Author Closing Comment

by:230409
ID: 31574029
thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 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