Question in shell scripting

Hello,

I have a scenario for which I am trying to write a shell script and I have a question regarding the same. Here's the situation:

I am trying to copy a directory which consists of a few sub-directories and .c and .dat extension files say n number of times so that the copied file have different ID's matching the list of ID's that I have. Now I could write a shell script to do that, however, I also need to write a shell script within the script so that one of the files, say n.dat which consists of a few variables is gonna get changed according to the ID of the directory it's copied into. There are 2 particular variables I am trying to modify, do you know if there are any commands in sed that can help me do it, I have tried the replace functionality but had no success so far.

any ideas?

Hi.

Welcome to the forum.

Thanks for text description of what you want.

I would help me to see a bit more graphically what you mean. For example, use command tree to help:

% tree -F d1
d1
|-- a.c
|-- a.out*
|-- b.c
|-- d11/
|   |-- (etc)
|   `-- f.c
`-- x.dat

Best wishes ... cheers, drl

Thanks for the response, here's the current code that exists:

#!/bin/bash

SOURCE_DIR="`dirname $1/.`"
user_id_match="@user_id@"

### userid loop
for user_id in {100..105}; do
  CP_DIR=${SOURCE_DIR}_${user_id}
  cp -r $SOURCE_DIR $CP_DIR

  USER_FILES="`ls $CP_DIR`";
  ### user file loop
  for ifile in $USER_FILES; do

      ORIG_FILE=${CP_DIR}/${ifile}
      NEW_FILE=${CP_DIR}/${ifile/${user_id_match}/${user_id}}

      if [ "$ORIG_FILE" != "$NEW_FILE" ]
      then
      echo mv $ORIG_FILE $NEW_FILE
      mv $ORIG_FILE $NEW_FILE
      fi

  done
done

Now, this is what the code is trying to execute, imagine a folder say Folder1 with a list of files labeled as file1, file2, file3, etc upto file n. Now, this script is copying the entire contents of the folder to another folder however the name of the folder has to changed correspondingly.

For example, if the original source folder which is called "Folder" is copied say 40 times into 40 different folders, the new folders should have the names Folder1, Folder2, Folder3 .... upto Folder40 and they shouldn't just be called "Folder" as the original source directory was called.

Now the script I had written copies the original source folder 40 times and names the new folders with folder1, folder2, etc as desired. Also some of the files inside each folder remain the same and some of them get renamed as file1,file2, ....upto file40(considering that the original file name in the source directory "Folder" was "file").

Now, here's what I am looking at, I have to provide a string substitution in the script(using sed perhaps) which can open a particular file, let's say for convenience, user.sh and replace one of the variables in the file(there are 5 in total) with the respective folder number. For example, the original source folder "Folder" contains a file called user.sh which has 5 variables var1,var2,var3,var4 and var5. Now let's say var1 has the particular folder number, for example in the original folder it's going to be 0 but when this is copied to Folder1, that has to be changed to 1, will be 2 when it's copied to Folder2 etc etc. What I am looking for is to find out how I can provide a string in the script to replace that particular variable with the folder number to which it's being copied onto.

It seems very complex to me as I am new to scripting, however some of the senior members here might have a better idea as to how to approach it.

Hi.

Apologies for late reply.

Before we continue, is this a homework assignment?

cheers, drl