Question in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question in shell scripting
# 1  
Old 03-08-2012
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?
# 2  
Old 03-08-2012
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:
Code:
% tree -F d1
d1
|-- a.c
|-- a.out*
|-- b.c
|-- d11/
|   |-- (etc)
|   `-- f.c
`-- x.dat

Best wishes ... cheers, drl
# 3  
Old 03-09-2012
Quote:
Originally Posted by drl
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:
Code:
% 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:
Code:
#!/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.

Last edited by Franklin52; 03-10-2012 at 06:48 AM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 03-13-2012
Hi.

Apologies for late reply.

Before we continue, is this a homework assignment?

cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question regarding for shell scripting.

I tried to run a command which simply generates SSH key with out prompting password. After several trails , got the below command to run the script : ssh-keygen -t rsa -N "" -f id_rsa -N "" tells it to use an empty passphrase (the same as two of the enters in an interactive script) -f... (0 Replies)
Discussion started by: ulaxmi
0 Replies

2. Shell Programming and Scripting

Shell Scripting question with SQL

hey i have to connect to sql through shell script , then store the value of the query in a variable and then compare it after some time after running a process. i have used this code but it is not working. #!/bin/sh Val = (sqlplus -s rte/rted2@rel76d2 <<! SELECT MAX(STAT_ID) FROM CVT_STATS;... (3 Replies)
Discussion started by: ramsavi
3 Replies

3. Shell Programming and Scripting

Shell Scripting Question

Hi guys, I am wanting to create a script that will logon to HackThisSite.org and complete Programming Mission 11. You can find a link to this mission here for your reference: www hackthissite org/missions/prog/11/ The following is what I have so far: #!/bin/bash USER="myUsername"... (0 Replies)
Discussion started by: spooke
0 Replies

4. Shell Programming and Scripting

Shell scripting question (Fedora 16)

Hi, I need to write a script that runs a series of commands in multiple subfolders. The problem is that I dont want to change directory for every folder. So this is why I thought to use a master script to activate the smaller ones. So, is there a way to command other scripts to open in... (9 Replies)
Discussion started by: Fuyudo
9 Replies

5. UNIX for Dummies Questions & Answers

shell scripting question

Testing for the presence/absence of a pattern in a file, using /bin/sh: while read a; do b="${a##*pattern*}"; ; done < file This returns 0 if there's a match. That signal ($?) can then be used outside the loop. However this method reads through the whole file, even if the match... (2 Replies)
Discussion started by: uiop44
2 Replies

6. Shell Programming and Scripting

scp shell scripting question

HI all, I am attempting to use the scp statemet in a shell script that I am writing I need to have the user of the script enter a password at the time of the shell script running.I have the following questions? 1) how do i disable the automatic text echo when the user is entering the... (1 Reply)
Discussion started by: Segwar
1 Replies

7. UNIX for Dummies Questions & Answers

Shell Scripting Question

Hi, I am assaigning the output to the variable outp and when i try to loop thru the variable i see the original content $OUTPUT also in the array outp. I dont want to have the original content in the array outp. Please reply. outp=$(echo $OUTPUT | awk '{FS = "|"}{ for(i=0;i<NF;i++)... (8 Replies)
Discussion started by: vijaykrc
8 Replies

8. UNIX for Dummies Questions & Answers

shell scripting newbie question

Hi all! I'm a newbie to shell scripting. I want to create a script that will store a line from a text file in a variable so I can then use it to open firefox with that text in the address bar (the text file contains a list of addresses). I have tried the following: #!/bin/sh a='sed -n 2p... (2 Replies)
Discussion started by: jazzman
2 Replies

9. Shell Programming and Scripting

Shell Scripting Question

To anyone who can help greetings, I am a beginner at Unix shell scripting. I need to know how to use the command line tool "itstat". I understand it's application, however I cannot find any additional information. Any assistance will be greatly appreciated. (3 Replies)
Discussion started by: tech2040
3 Replies

10. Shell Programming and Scripting

Shell Scripting Question

To anyone who can help greetings, I am a beginner at Unix shell scripting and am taking a class. The assignment question is as follows: use a command-line tool named "itstat" which will display the resolution of an image file and some other lines of information. It accepts a list of image... (1 Reply)
Discussion started by: tech2040
1 Replies
Login or Register to Ask a Question