How to create a shell script to read a foldername from a text file and go to the folder?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Linux How to create a shell script to read a foldername from a text file and go to the folder?

Hi,
I am trying to write a shell script which can read folder names from a text file and then go to the folder and picks up a xml file and write on my sipp script so that I can run the sipp script.

For example:

I have a text file called thelist.txt where I have provided all the folders name in which I have kept xml files for different purposes.

I want to write a shell script which can read the text file and then go to individual folders and picks up the xml file and write it into a sipp command.
It should be done 1000 times if I have 1000 names in the text files and I want to execute the script for 1000 xml files.

Please help.
# 2  
THIS will give you an idea about parsing a file line-by-line.

Next switching to directories can be achieved by cd command.

I don't know what do you mean by sipp command! Smilie

Can you please give example?
# 3  
Code:
 
while read foldername
do
      cd ${foldername}
      for i in *.xml
      do
            echo "file name : ${i}"
            #write your sipp command here
      done
done < thelist.txt

make sure, you have the absolute path in the thelist.txt
# 4  
Linux How to create a shell script to read a foldername from a text file and go to the folder?

Here is the code which is just reading the name from testlist.txt and I have given the absolute path of the directory where it should read the .xml from.

Now I have achived my first hurdle. I want to execute the scripts one by one and not parallel. currently its executing all the scripts in parallel.

Please suggest

My Code:

Code:
for i in `cat testlist.txt`

     do

#       if [msml/$i/$i.xml]
#       if [-f $i]
#     then

       sipp $2 -i $1 -m 1 -l 1 -s msml -sf msml/$i/$i.xml --trace_logs -timeout $3
#     fi
i++
done
~


Last edited by DukeNuke2; 01-25-2013 at 07:02 AM..
# 5  
First off that is a dangerous use of backticks, much better to rewrite your loop as

Code:
while read LINE
do
...
done < inputfile

Second, to run a command in the background, simply put an & at the end of it.

Code:
sipp $2 -i $1 -m 1 -l 1 -s msml -sf msml/$i/$i.xml --trace_logs -timeout $3 &

Third of all, unless your computer has 1000 cores, no point in running 1000 processes simultaneously, they will just go slower. You ought to wait after doing a certain number. Not all shells have facilities to wait for a particular process, the following script requires bash or newer ksh.

Code:
#!/bin/bash

# Save for later since set -- overwrites these
A=$1
B=$2
C=$3
D=$4
E=$5

set --

while read i
do
        sipp $B -i $A -m 1 -l 1 -s msml -sf msml/$i/$i.xml --trace_logs -timeout $C &

        # Use $1 $2 ... as a list of PIDs.  The latest PID gets put on the end.
        set -- $* $!

        # If we have more than 3 processes going, wait for the oldest one.
        if [ "$#" -gt 3 ]
        then
                wait $1
                shift
        fi
done < inputfile

wait

Last of all, if these processes are disk-intensive, running them in parallel may not make them faster anyway.
This User Gave Thanks to Corona688 For This Post:
# 6  
How to create a shell script to read a foldername from a text file and go to the folder?

Thanks a lot for suggesting me the good practices. I will make sure I take care of these good practices in my script.

What is the good way to execute one script at a time?

sipp command goes in text file "testlist.txt" and it reads one folder name and it goes in the folder and reads .xml file execute the script and writes the result in a result file and then jump to the next script.

Thanks
# 7  
Running them one at a time?

That's what you were doing already Smilie
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create new folder and text file the same time in one line

Is it possible to create new folder and write a new text file in the folder while the current position is outside the new folder? in one line mkdir folder | echo "hello test"> folder/test.txt not work. (1 Reply)
Discussion started by: cmdcmd
1 Replies

2. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

3. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

4. Shell Programming and Scripting

Script to create folder, copy file, and send email on success

I am very new to UNIX as well as scripting so please be gentle, haha. I have a Linux client which has a mount point mapped to my /etc folder on a NetApp FAS system. I woudl like to be able to copy the hosts and rc files once a week from the FAS to a different location for backup. When the... (4 Replies)
Discussion started by: minnino
4 Replies

5. Shell Programming and Scripting

Need to build Shell Script to search content of a text file into a folder consist several files

Have to read one file say sourcefile containing several words and having another folder containing several files. Now read the first word of Sourcefile & search it into the folder consisting sevral files, and create another file with result. We hhave to pick the filename of the file in which... (3 Replies)
Discussion started by: mukesh.baranwal
3 Replies

6. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

7. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

8. Shell Programming and Scripting

How to read the data from the text file in shell script?

I am having one text file and i need to read that data from my shell script. I will expain you the scenario: Script look like: For name type 1: For age type 2: For Salary type3: echo "Enter the input:" read the data if input is 1 then go to the Text file and print the... (2 Replies)
Discussion started by: dineshmurs
2 Replies

9. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies

10. Shell Programming and Scripting

shell script to read data from text file and to load it into a table in TOAD

Hi....can you guys help me out in this script?? Below is a portion text file and it contains these: GEF001 000093625 MKL002510 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL003604 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL005675 000001... (1 Reply)
Discussion started by: pallavishetty
1 Replies

Featured Tech Videos