Visit Our UNIX and Linux User Community


UNIX Shell Scripting (Solaris) for File Checking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX Shell Scripting (Solaris) for File Checking
# 1  
Old 05-30-2016
Wrench UNIX Shell Scripting (Solaris) for File Checking

Hi guys,

I'm sorry but i badly need your help. I am assigned to do a basic shell script in my job but sadly i don't have any idea on what it is because i am an electronics engineer, but i googled all of it, ask my friends but i cant finalize my scripts. so do please help me.

The requirement is i hve to do a shell script in unix solaris that will check invalid files in the directory. when there is an invalid file. it will echo "there is an invalid file in this directory(=the directory itself) please see dummyevents.log." then is no invalid file there. it will echo"there is no invalid file in this directory(=the directory itself)". then it will not produce dummyevents.log

how will you knwo that it is invalid file? ihave list of words like. if there is no "apple" word inside the file in the directory. it is invalid.

i hope you understand guys. and please see below draft of my scripts.

Code:
apple_DIR=/home/cfg/dirAPPLE.cfg
banana_DIR=/home/cfg/dirBANANA.cfg
orange_DIR=/home/cfg/dirORANGE.cfg
RESULT=/home/
apple_file=`cat $APPLE_DIR`
banana_file=`cat $BANANA_DIR`
orange_file=`cat $ORANGE_DIR`
#apple
for a in $apple_file
    do 
	cd $a
	touch Statuscheck.tmp
	ls -lrt | grep -l apple > Statuscheck.tmp
    FILE=`cat Statuscheck.tmp | wc -l`
    if [ $FILE -ne 0 ]
	then
	   echo "Invalid file in $a directory" > $RESULT/dummyevents.log
    fi
    rm Statuscheck.tmp
    done
#banana
for b in $banana_file
    do 
	cd $b
	touch Statuscheck.tmp
	ls -lrt | grep -l banana > Statuscheck.tmp
    FILE=`cat Statuscheck.tmp | wc -l`
    if [ $FILE -ne 0 ]
	then
	   echo "Invalid file in $b directory" > $RESULT/dummyevents.log
    fi
    rm Statuscheck.tmp
    done
#orange
for c in $orange_file
    do 
	cd $c
	touch Statuscheck.tmp
	ls -lrt | grep -l orange > Statuscheck.tmp
    FILE=`cat Statuscheck.tmp | wc -l`
    if [ $FILE -ne 0 ]
	then
	   echo "Invalid file in $c directory" > $RESULT/dummyevents.log
    fi
    rm Statuscheck.tmp
    done


Last edited by Don Cragun; 05-30-2016 at 07:29 AM.. Reason: Add missing CODE tags.
# 2  
Old 05-30-2016
Not sure if I understood correctly. Let me paraphrase:
You have a handful of directories with some files in each. You want to check if those files have exactly ONE keyword in them, each directory having a different keyword. While the files to be searched are supplied in a config file, one per directory, the keywords are NOT but are constants. Correct?

Some comments upfront:
- *nix differentiates between cases, so apple_DIR does NOT equal APPLE_DIR.
- you can't cat directories (cat $APPLE_DIR) - or is that variable name just misleading?
- with identical code pieces (as shown for the different fruits), loops or subroutines/functions lend themselves for implementation.

Other optimization potential we can discuss once the requirements are clear.


EDIT: And, of course: Please use code tags as required by forum rules!
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-30-2016
i'm so sorry for the confusion.

in exact i have 7 directories that i need to check. let me give an example.

first directory /home/banana and this directory has files which has "BANANA" word in it. so what they want me to do is to detect the file in the directory WITHOUT "BANANA" word in it because it is considired an ivnalid file in the directory. when file(s) WITHOUT "BANANA" is/are detected it will echo "there is invalid files in this directory. please see dummyevents.log"

and the resutls of the script is the dummyevents.log which when you view it, will contain the directories who has a invalid file and the filename of the invalid file.

i hope you understand.

also you can diregard my draft script and suggest another script construction or scipt approach.. thanks.

Last edited by daveaztig14; 05-30-2016 at 09:42 AM.. Reason: Disregarding draft sciprt
# 4  
Old 05-30-2016
Could you post an example of your config data, or are you happy with any (new) structure of config file(s)?
Any subdirectories under e.g. /home/banana?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-30-2016
And, keyword always the same as the directory name? Should the search respect case or not?
Where should the config file reside?
This User Gave Thanks to RudiC For This Post:
# 6  
Old 05-30-2016
You failed to mention which shell you are using. With a not too old bash that has extended pattern matching with the extglob shell option, and a config file with directory and keyword in it like
Code:
./apple         tart
./orange        juice
./banana        banana

this might already do the trick:
Code:
while read DIR KW REST; do cd $DIR; echo invalid files in $DIR: $(ls !(.|..|$(grep -l $KW *))); cd ..; done < dave.cfg;
invalid files in ./apple: X y
invalid files in ./orange: Z
invalid files in ./banana: x

Of course, this is not yet bullet proof, as there's no error checking yet built in, and the positive message is missing, too.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-30-2016
I hope this is not coursework/homework.
And I assume that you want to check the file names (not the file data)
Code:
basedir=/home
log=$basedir/dummyevents.log

for word in banana apple orange
do
  dir=$basedir/$word
  badfile=0
  
  for fname in $dir/*
  do
    if [ -f "$fname" ]
    then
      case "$fname" in
      *$word*) # these are ok
      ;;
      *) # all others
        echo "Invalid filename '$fname'" # >> $log
        # not redirected here, for efficiency we redirect the whole loop
        badfile=1
      ;;
      esac
    fi
  done >> $log
  
  if [ $badfile -eq 1 ]
  then
    echo "Invalid file in $dir, see $log"
  fi
done


Last edited by MadeInGermany; 05-30-2016 at 06:03 PM.. Reason: Added missing $
This User Gave Thanks to MadeInGermany For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Solaris Shell Scripting

Hi, Want to know, is there any way to restrict a Solaris user to Single Login. Means a particular user can login once and if he or someone else tries to login with his ID then a message displayed "user already logged in" and denies his attempt. Regard, Jeet (1 Reply)
Discussion started by: CountJeet
1 Replies

2. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

3. Shell Programming and Scripting

How to add trailer record at the end of the flat file in the unix ksh shell scripting?

Hi, How to add trailer record at the end of the flat file in the unix ksh shell scripting can you please let me know the procedure Regards Srikanth (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

4. Shell Programming and Scripting

Request for file read option in Unix shell scripting

Hi Friends, I would like to read all the record from one txt file to other file txt For example I have two txt file a.txt and b.txt. I need to read a.txt record by record and I need add the system date @ end of each record before moving it to b.txt. Could you please share the coding for... (4 Replies)
Discussion started by: vinoth124
4 Replies

5. Shell Programming and Scripting

generate tabular output from an input text file in unix shell scripting

Hi, I have the output (as below) which i want it to be in a table. For e.g. space utilization in PSE on path /logs is 0% space utilization in PSE on path /logs/tuxedo/tuxlsp is 16% space utilization in PSE on path /ldvarlsp/lsp/log is 37% space utilization in PSE on path /home is 6%... (7 Replies)
Discussion started by: pkbond
7 Replies

6. Shell Programming and Scripting

C Shell Scripting - HELP! - checking total args in a script

Hi, I 'm trying to learn the scripting language and am trying to create a script to open a C Program, allow the user to edit it, and then run it. What I have works but only when you enter the name to be compiled and the c program, but what if you only entered the 1 argument (cprogram.c) ? but I 'm... (3 Replies)
Discussion started by: patel_ankz
3 Replies

7. UNIX for Dummies Questions & Answers

Unix Shell Scripting -- update employees not present in input file

ALL, My shell script takes a employee file as input. I have to identify the list of employees not in the input file and update their status in the database. Approach I followed: by traversing through the input file add all the emplid's to a variable. update the status of employees not in... (2 Replies)
Discussion started by: sailussr
2 Replies

8. Solaris

How to check the file existence using shell scripting in Solaris-10

Hi, I have a script which will check the fiel existence, the lines are as below if !(test -d ./data) then mkdir data fi In the first line error occurs as below generatelicense.sh: syntax error at line 2: `!' unexpected Where as this script works fine in linux OS. How to solve... (2 Replies)
Discussion started by: krevathi1912
2 Replies

9. AIX

Unix shell scripting to find latest file having timestamp embedded...

Hi guys, I have a directory in UNIX having files with the below format, i need to pickup the latest file having recent timestamp embedded on it, then need to rename it to a standard file name. Below is the file format: filename_yyyymmdd.csv, i need to pick the latest and move it with the... (2 Replies)
Discussion started by: kaushik25
2 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

Featured Tech Videos