ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm am a newbie to coding so I am reaching out in hopes that I can get some help from this forum.
I am trying to run the script below from a single directory, however the directory has many subfolders where the file I need is located. I have bolded the file name below also.
Another thing is that for me to run this script successfully, at the moment I have to type in the actual path to the .ecclog file in terminal like so
Code:
scriptname.sh /TestFolder/.extension
Is there any way I can make the script run through all the subfolders and find the file it needs without having to type in the actual path in terminal? I have about 160 of these folders and manually running the script on each of those folders one by one would take forever. I'm looking for an easier way and I hope someone here can help me out!
#!/bin/bash
working_dir="put search path here - parent/root dir name"
while read FILENAME
do
add code in here for file processing-
it will go through each file separately - recursively
done< <(find "$working_dir" -type f -name "*.ecclog")
this way you can have the script anywhere in your system and when you run it it will still search wherever the path is set to look.
the value within FILENAME is a complete absolute path and filename. you might have to break it down to work with it using
#!/bin/bash
working_dir="put search path here - parent/root dir name"
while read FILENAME
do
add code in here for file processing-
it will go through each file separately - recursively
done< <(find "$working_dir" -type f -name "*.ecclog")
this way you can have the script anywhere in your system and when you run it it will still search wherever the path is set to look.
the value within FILENAME is a complete absolute path and filename. you might have to break it down to work with it using
That will run scriptname.sh with a parameter of every file ending in .ecclog from the current directory downwards.
If I understand correctly, your suggestions will make the script go into every subfolder and grab the .ecclog file unique to that subfolder and it will continue to do so until it has gone through and ran on all the 160 subfolder that I have? I ask because I forgot to mention that every subfolder that I have, has inside it a uniquely numbered file that ends in .ecclog.
If I understand correctly, your suggestions will make the script go into every subfolder and grab the .ecclog file unique to that subfolder and it will continue to do so until it has gone through and ran on all the 160 subfolder that I have? I ask because I forgot to mention that every subfolder that I have, has inside it a uniquely numbered file that ends in .ecclog.
Yes, it will. If you want to test what it does, you can put echo in to see what will be run, like this:
the only big difference between what I wrote and this one is placement of the "command/script' when ran - my code was so you can run the script from anywhere, whereas this line of code you have to be within the parent directory, that can be ealiy changed to.
that too should work. if the while loop then within your coe a few lines could be removed to accommodate the loop, that basename part because the other chunk of code
Don't try to do it in bash. Every "real" scripting language that I can think of has a file-finder. Simply choose the language you like, and insert a #!shebang line as the first line of your script identifying that language. Then, write the "shell script" in that language and you're done.
#!/bin/bash
working_dir=
while find FILENAME
do
f=$FILENAME
path=${f%/*}
xfile=${f##*/}
title=${xfile%.*}
ext=${xfile##*.}
basenm=$title
#searching for the keyword 'Final' inside file using complete path and file name.
nums=$(grep -n 'Final' $FILENAME | sed 's/:.*//')
the rest of your code...
done< <(find "working_dir" -type f -name "*.ecclog" )
or this
Code:
#!/bin/bash
working_dir=
while find FILENAME
do
basenm=$(basename $FILENAME .ecclog)
#searching for the keyword 'Final' inside file using complete path and file name.
nums=$(grep -n 'Final' $FILENAME | sed 's/:.*//')
the rest of your code...
done< <(find "working_dir" -type f -name "*.ecclog" )
Yes it will find all files ending in 'ecclog' anywhere below the leading directory
Okay, I ran the following in terminal but received an error (see screenshot). I'm not sure what I'm doing wrong. Please excuse me if it's something blatantly obvious -- I'm very new to scripting so you guys might have to really walk me through it baby steps.
Code:
find . -name '*.ecclog' -exec ec_plot.sh {} \;
Here's a screenshot of my directory and the error I received in terminal.
Okay, I ran the following in terminal but received an error (see screenshot). I'm not sure what I'm doing wrong. Please excuse me if it's something blatantly obvious -- I'm very new to scripting so you guys might have to really walk me through it baby steps.
Code:
find . -name '*.ecclog' -exec ec_plot.sh {} \;
Here's a screenshot of my directory and the error I received in terminal.
Several possible issues:
You may need to put the full pathname of the script in the find command, so instead of just ec_plot.sh, it'd be /path/to/ec_plot.sh
Or maybe your shebang isn't right in ec_plot.sh itself. Make sure you have
Code:
#!/bin/bash
or whatever is appropriate as the first line in your script. You can find out where bash is installed on your system by typing in:
Code:
which bash
Plus make sure the script is set to be executable:
Code:
chmod 755 ec_plot.sh
edit:
Ooh, I just saw your script, and the shebang problem is almost certainly it! You have #!/bin/sh which probably doesn't exist on your system, replace it with #!/bin/bash
You may need to put the full pathname of the script in the find command, so instead of just ec_plot.sh, it'd be /path/to/ec_plot.sh
Or maybe your shebang isn't right in ec_plot.sh itself. Make sure you have
edit:
Ooh, I just saw your script, and the shebang problem is almost certainly it! You have #!/bin/sh which probably doesn't exist on your system, replace it with #!/bin/bash
the script is executable and I fixed the shebang as you suggested. Now, I get the following error.
If I'm right, and /bin/sh isn't there, you can also copy /bin/bash to /bin/sh instead of changing your script. Bash actually changes its behavior slightly when ran as sh to be as compatible as possible with the original sh. If the script was written to run in sh, you could run into problems when running it in bash when not in sh emulation mode. (Although the differences are not very many)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.