Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 10022  / 2 Years ago, mon, september 26, 2022, 11:22:42

I am making a script to simplify my daily tasks. Everyday, I have to grep for a few things inside a company server. It was okay, however, now, they have segregated each object into sub directories. I am looking for a solution in which my existing shell script will execute repeatedly into each sub directory inside a certain directory. How do I do this? I am quite new to Ubuntu and still learning the ropes.



Here's my script:



#!/bin/bash

#fetch start time of uut
grep -i "01_node_setup" his_file | tail -1 >> /home/xtee/sst-logs.out

#check if sysconfig.out exists
if [ -f sysconfig.out];
then
grep -A 1 "Drive Model" sysconfig.out | tail -1 >> /home/xtee/sst-logs.out
else
grep -m 1 "Pair0 DIMM0" node0/trans_file_prev/*setupsys* | tail -1 >> /home/xtee/sst-logs.out
fi


Basically I want to run this script to execute on all the existing sub directories of a certain directory. What do I do? Thanks!


More From » bash

 Answers
3

This should more or less do the trick. I am assuming that the 'node0' dir will be within the subdirectories if 'sysconfig.out' is not and that 'his_file' is also in the subdirectories.



#!/bin/bash
MainDir=/path/to/dir/containing/subdirs
# cd into dir
cd $MainDir

for dir in *; do

#fetch start time of uut
grep -i "01_node_setup" $dir/his_file | tail -1 >> /home/xtee/sst-logs.out

#check if sysconfig.out exists
if [ -f $dir/sysconfig.out];
then
grep -A 1 "Drive Model" $dir/sysconfig.out | tail -1 >> /home/xtee/sst-logs.out
else
grep -m 1 "Pair0 DIMM0" $dir/node0/trans_file_prev/*setupsys* | tail -1 >> /home/xtee/sst-logs.out
fi

done

#cd back to where we started
cd $OLDPWD

[#36428] Monday, September 26, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
igeonlothe

Total Points: 370
Total Questions: 121
Total Answers: 114

Location: United States Minor Outlying Island
Member since Fri, Feb 5, 2021
3 Years ago
igeonlothe questions
Wed, May 31, 23, 02:34, 1 Year ago
Sat, Mar 12, 22, 17:13, 2 Years ago
Tue, Aug 31, 21, 09:46, 3 Years ago
;