How to include functions from a shell script from a different folder - shell

I have a directory structure of type
|--bringup-scripts
| |--prep.sh
|--scripts
| |--i2c0.func
| |--bit.func
The prep.sh looks like this :
#!/bin/bash
. ../scripts/i2c0.func
The i2c0.func looks like this :
#!/bin/sh
. ./bit.func
As there is a dependency of i2c0.func on bit.func ,If I run prep.sh from the bringup-scripts folder,it throws an error saying
./bit.func: No such file or directory
How should I resolve this?

What you could do is not using relativ paths, instead defining a starting point for all scripts in the prep.sh file like this:
#!/bin/bash
export ROOT_DIR="$(dirname $PWD)"
. $ROOT_DIR/scripts/i2c0.func
You have to include $ROOT_DIR as a prefix for every script in the scripts directory too, e.g.:
i2c0.func
#!/bin/sh
. $ROOT_DIR/scripts/bit.func

I don't know if there's a standard way to do this, but you could try changing i2c0.func like this:
#!/bin/sh
. ../scripts/bit.func
Though note prep.sh gets its caller's working directory, so it will only work when you run it from bringup-scripts/ or scripts/.

Related

Perl code doesn't run in a bash script with scheduling of crontab

I want to schedule my Perl code to be run every day at a specific time. so I put the below code in bash file:
Automate.sh
#!/bin/sh
perl /tmp/Taps/perl.pl
The schedule has been specified in below path:
10 17 * * * sh /tmp/Taps/Automate.sh > /tmp/Taps/result.log
When the time arrived to 17:10 the .sh file hasn't been running. however, when I run ./Automate.sh (manually) it is running and I see the result. I don't know what is the problem.
Perl Code
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
use XML::Dumper;
use TAP3::Tap3edit;
$Data::Dumper::Indent=1;
$Data::Dumper::Useqq=1;
my $dump = new XML::Dumper;
use File::Basename;
my $perl='';
my $xml='';
my $tap3 = TAP3::Tap3edit->new();
foreach my $file(glob '/tmp/Taps/X*')
{
$files= basename($file);
$tap3->decode($files) || die $tap3->error;
}
my $filename=$files.".xml\n";
$perl = $tap3->structure;
$dump->pl2xml($perl, $filename);
print "Done \n";
error:
No such file or directory for file X94 at /tmp/Taps/perl.pl line 22.
X94.xml
foreach my $file(glob 'Taps/X*') -- when you're running from cron, your current directory is /. You'll want to provide the full path to that Taps directory. Also specify the output directory for Out.xml
Cron uses a minimal environment and a short $PATH, which may not necessarily include the expected path to perl. Try specifying this path fully. Or source your shell settings before running the script.
There are a lot of things that can go wrong here. The most obvious and certain one is that if you use a glob to find the file in directory "Taps", then remove the directory from the file name by using basename, then Perl cannot find the file. Not quite sure what you are trying to achieve there. The file names from the glob will be for example Taps/Xfoo, a relative path to the working directory. If you try to access Xfoo from the working directory, that file will not be found (or the wrong file will be found).
This should also (probably) lead to a fatal error, which should be reported in your error log. (Assuming that the decode function returns a false value upon error, which is not certain.) If no errors are reported in your error log, that is a sign the program does not run at all. Or it could be that decode does not return false on missing file, and the file is considered to be empty.
I assume that when you test the program, you cd to /tmp and run it, or your "Taps" directory is in your home directory. So you are making assumptions about where your program looks for the files. You should be certain where it looks for files, probably by using only absolute paths.
Another simple error might be that crontab does not have permission to execute the file, or no read access to "Taps".
Edit:
Other complications in your code:
You include Data::Dumper, but never actually use that module.
$xml variable is not used.
$files variable not declared (this code would never run with use strict)
Your $files variable is outside your foreach loop, which means it will only run once. Since you use glob I assumed you were reading more than one file, in which case this solution will probably not do what you want. It is also possible that you are using a glob because the file name can change, e.g. X93, X94, etc. In that case you will read the last file name returned by the glob. But this looks like a weak link in your logic.
You add a newline \n to a file name, which is strange.

Replace string path in log file with current folder name

I'm new here and already tried to find solution to the following requirement without success. I'm trying to achieve this:
I have these 5 folders:
ServiceEngine
PaymentEngine
InvoiceEngine
ProcessEngine
OrderProcessEngine
Inside each of these folders, I have a log file with default path location to store the log files e.g. ServiceEngine/logs
The log file contain the following path structure:
name="RollingRandomAccessFile" fileName="logs/engine.log"
filePattern="logs/engine-%i.log"
I expect to find a way that I retrieve the name of the current folder which I'm in and replace the string engine with folder name
Example: I'm in ServiceEngine folder and execute a command that retrieve the current folder name. The expected result is:
name="RollingRandomAccessFile" fileName="logs/ServiceEngine.log"
filePattern="logs/ServiceEngine-%i.log
Later I change the directory to PaymentEngine and the expected result is:
name="RollingRandomAccessFile" fileName="logs/PaymentEngine.log"
filePattern="logs/PaymentEngine-%i.log
and so on. Maybe there is a smarter way to create a script that update the string in a loop like do; if ... fi; done or to use the for in ... loop.
Do you mean something like this?
~/ServiceEngine$ cat logfile
name="RollingRandomAccessFile" fileName="logs/engine.log"
filePattern="logs/engine-%i.log"
~/ServiceEngine$ awk -v path=$(basename $(pwd)) 'gsub("engine", path)' logfile
name="RollingRandomAccessFile" fileName="logs/ServiceEngine.log"
filePattern="logs/ServiceEngine-%i.log"
See basename, declaring variables in awk and awk gsub.
I guess I don't understand your question, but:
dir=$( basename $( pwd ) )
echo name="RollingRandomAccessFile" \
fileName="logs/$dir.log" \
filePattern="logs/$dir-%i.log"
Is that what you're looking for?
It sounds like you are looking for something like this:
$ sed 's/\bengine\b/'$(basename $(pwd))'/' logs
When run from within one of your folders, it spits out the text you're asking for. It wasn't clear what you wanted to do with that text though.

Simple Bash Script: Change names of files to mimic directories

I have 312 directories labeled,
Ion_0001- Ion_0312.
In each directory I have a file light.out. I'd like to change the file names in each directory to, for example:
Ion_0001.out
I believe I also need to substitute the / so that my output DOESNT look this this:
Ion_0001/.out
Can any one help me out with a simple script??
This is what I've tried:
#!/bin/bash
for dir in */
do
cd $dir
for filename in *.out; do
mv $filename ${filename//$dir.out}
done
cd ..
done
Thanks!
Not a free coding service, but it's simple enough to not make it worth arguing about...
Assuming this file structure:
Ion_0001/
Ion_0001/light.out
Ion_0002/
Ion_0002/light.out
...
Run this code in a script or just at the command line:
for i in Ion_0*
do
mv "${i}/light.out" "${i}/${i}.out"
done
Resulting in this structure:
Ion_0001/
Ion_0001/Ion_0001.out
Ion_0002/
Ion_0002/Ion_0002.out
...
Is that what you were looking for?
for dir in Ion*/; do
mv "${dir}light.out" "${dir}${dir%/}.out"
done
The trailing slash in the Ion*/ pattern limits the results to directories only, but the slash will be present in the variable's value.

What is wrong in the for loop I use to create a file source=dest list?

I try to loop over the file I findin a relative path to build a list of relative path/soure file name=source file name
SHARED_LIB_PACK=""
for LIB in $(find ../level1/leve2/ -name "*.so*")
do
$SHARED_LIB_PACK=$SHARED_LIB_PACK" "$LIB"="${LIB##*/}
done
but as I run it, it complain :
line 6: = ../level1/level2/file.so.1.0=file.so.1.0: No such file or directory
Any help will be welcome
Firstly, variable assignment is done via:
FOO="bar"
and not
$FOO="bar"
The former will not work.
Secondly, your quotes seem to be in strange places:
SHARED_LIB_PACK=$SHARED_LIB_PACK" "$LIB"="${LIB##*/}
should probably be
LIB="${LIB##*/}"
SHARED_LIB_PACK="$SHARED_LIB_PACK $LIB"
or
SHARED_LIB_PACK="$SHARED_LIB_PACK ${LIB##*/}"

Call partial file name in a linux script

I have several files: one_001_three.txt, one_002_three.txt, . . .
After removing the extension, I would like to call it such that:
${fname}_001
would call the file 'one_001'
Any ideas?
As far as i understood your Question ..
yu can use this command basename try with this and develop .
basename one_001_three.txt _three.txt -->this will give output as one_001 .
the filename doesnt get changed though .
As far as i understood your Question ..
you can use this command basename try with this and develop .
basename one_001_three.txt _three.txt -->this will give output as one_001 .
the filename doesnt get changed though

Resources