views:

343

answers:

2

Hi

I want to write a script that will:

1- locate folder "store" on a *nix filesystem

2- move into that folder

3- print list of contents with last modification date

4- calculate sub-folders size

This folder's absolute path changes from server to server, but the folder name remains the same always.

There is a config file that contains the correct path to that folder though, but it doesn't give absolute path to it.

Sample Config:


Account ON

DIR-Store /hdd1

Scheduled YES


ِAccording to the config file the absolute path would be "/hdd1/backup/store/"

I need the script to grep the "/hdd1" or anything beyond the word "DIR-Store", add "/backup/store/" to it, move into folder "store", print list of its contents, and calculate the sub-folder's size.

Until now I manually edit the script on each server to reflect the path to the "store" folder.

Here is a sample script:

    #!/bin/bash

echo " "

echo " "

echo "Moving Into Directory"

cd /hdd1/backup/store/

echo "Listing Directory Content"             

echo " "

ls -alh

echo "*******************************"

sleep 2

echo " "

echo "Calculating Backup Size"

echo " "

du -sh store/*

echo "**********   Done!   **********"

I know I could use grep

cat /etc/store.conf | grep DIR-Store

I just don't know how to get around selecting the path, adding the "/backup/store/" and moving ahead.

Any help will be appreciated

A: 

You can use cut to extract columns from the configuration file. Specify a field delimiter with -d. Cut only allows single-character delimiters (like e.g. a single space) and there are certainly gazillion other ways to split the line.

Then just manually append the know subdirectory to the stem.

STORE=$(grep DIR-Store /etc/store.conf | cut -d" " -f2)
DIR="${STORE}/backup/store"

pushd "${DIR}"
ls -alh
sleep 2
du -sh *
popd
honk
@ honkYour approach gave me a: line 6: unexpected EOF while looking for matching `"'I tired replacing the space between quotes in ( -d" " ) with a tab, still didn't workBut Thank you!
Lithiumion
@Lithium: fixed missing quota
honk
@ honkThank you so much. now it works. Didn't notice the missing quota!Still, I had to remove the /backup from the line DIR="${STORE}/backup/store"Otherwise it looks for the directory at /backup/backup/store.Thanks again
Lithiumion
A: 

If there are no spaces on that line except for the one(s) between "DIR-Store" and the directory:

dir=($(grep "DIR-Store" /etc/store.conf))
dir="${dir[1]}/backup/store"
cd "$dir"    # or pushd "$dir"

or this keys on the first slash rather than a space:

dir=$(grep "DIR-Store" /etc/store.conf)
dir="/${dir#*/}/backup/store"
cd "$dir"    # or pushd "$dir"
Dennis Williamson
@ Dennis WilliamsonThis works!Just had to edit the line dir="${dir[1]}/backup/store"to bedir="${dir[1]}/store"Then it excuted just fine.Thank you a ton!
Lithiumion