LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash scripting (https://www.linuxquestions.org/questions/programming-9/bash-scripting-86505/)

-=MaGo=- 08-28-2003 02:52 AM

bash scripting
 
I need to write a bash script to run a program I have written

I have this problem: The script has top check if the executable file (frbv.0.85) is in the current directory:

if [ -f frbv.0.85]; then
frbv.0.85
fi

I want the script checks if frbv.0.85 is in the PATH before exiting with an error

:Pengy: any advice?

thanks

Diego

vanquisher 08-28-2003 05:29 AM

could you state the problem more clearly? if you want to check for an executable EXEC in directory PATH, why can't you say
Code:

if [ -f PATH/EXEC]
then
    ./PATH/EXEC
fi

if it's not what you are looking for, may be I should read the question again.

vanquisher 08-28-2003 05:30 AM

could you state the problem more clearly? if you want to check for an executable EXEC in directory PATH, why can't you say
Code:

if [ -f PATH/EXEC]
then
    ./PATH/EXEC
fi

if it's not what you are looking for, may be I should read the question again.

Hko 08-28-2003 07:37 AM

Quote:

I want the script checks if frbv.0.85 is in the PATH before exiting with an error
Code:

TST=$(which frbv.0.85)
if [ -x "$TST" ] ; then
    frbv.0.85
fi

The command "which frbv.0.85" finds the executable "frbv.0.85" by searching your $PATH and echoes the entire path, including the filename itself, to the standard output. E.g. which gcc echoes "/usr/bin/gcc".

Putting a command inside $( ) replaces it with the output of the command inside $( ). And then bash executes the line. E.g. when you have a line "TST=$(which gcc)", bash will execute the command "which gcc". then it replaces "$(which gcc)" with the output of "which gcc". After that it executes the line of script-code, in this case "TST=/usr/bin/gcc".

So the TST variable contains the entire path of the executable where it was first found in $PATH. And now you can test if the file found is executable with "[ -x "$TST]".

-=MaGo=- 08-28-2003 07:37 AM

I will try to be more clear :D

the name of the exe file is frbv.0.85
to run this program it has to be in the work directory.
In this case I can run it in this way:

$ ./frbv.0.85

if the file is not in the directory there must be a copy of frbv.0.85 in a drectory that is in the path, in this case I can run it in this way:

$ frbv.0.85

I want to check if frbv.0.85 is in the eork directory and in this case I will run it:

if [ -f frbv.0.85]; then
./frbv.0.85
fi

if frbv.0.85 is not in the work directory I want to check if there is a file frbv.0.85 in the path in this case I will run it

if [ ?????? ]; then
frbv.0.85
fi

I do not know how to check if there is a copy of frbv.0.85 in a directory that is in my path

thaks again :D

Hko 08-28-2003 07:59 AM

We posted at exactly the same time.
See my previous post, it solves your problem.

-=MaGo=- 08-28-2003 11:06 AM

thanks for your advice!

:-D

-=MaGo=-

Skyline 08-28-2003 03:25 PM

Off topic guys... but rather than starting a new thread does any body know the relevant commands to insert blank lines when concatenating files - ie if you had

cat a b > c ; d >> c

were a,b,c,d are ordinary text files - the content of the resulting file c is scrunched up - I would like each individual "file" in c to have a blank line between them.

TIA

Strike 08-28-2003 03:32 PM

echo -e "\n\n" >> file

will insert two blank lines

Skyline 08-28-2003 03:36 PM

Cheers Strike - that's just what I'm looking for - nice one!

Skyline 08-28-2003 05:57 PM

Is there a more efficient way using the shell to separate files using blank lines in the concatenated file? This seems to long winded, even for the shell.

m() { echo -e "\n\n"; } ; cat b > a ; m >> a ; cat c >> a ; m >> a ; cat d >> a ; m >> a ; .................. etc

were b,c,d,........ are files and a is the "concatenated file"

slapNUT 08-28-2003 11:18 PM

Suppose you had a file e that only had two empty lines.


cat a e b e > c

This might clear things up slightly.

Skyline 08-29-2003 12:36 AM

Thanks for the tip Slapnut - just learning at the moment - ( 4 days old - and still very inefficient !! - new to bash scripts/language etc ) so am just trying a few ideas out explicitly at the moment.

In the shell just for learning :

m() { echo -e "\n\n"; } ; cat b > a ; m >> a ; cat c >> a ; m >> a ; cat d >> a ; m >> a ; .................. etc

were b,c,d,........ are files and a is the "concatenated file"


Actually - instead of constantly typing the path to the "concatenated" file "a", I might aswell just assign this to a variable "x" at the start along with the simple function "y"


x=”a” ; y() { echo -e "\n\n"; } ; cat b > $x ; y >> $x ; cat c >> $x ; y >> $x ; cat d >> $x ; y >> $x........etc

were a is the final file and b,c,d.......etc are the files that go to make it up - putting something concrete in:


x="/home/mikki/first" ; y() { echo -e "\n\n"; } ; cat /etc/lilo.conf > $x ; y >> $x ; cat /etc/fstab >> $x ; y >> $x ; fdisk -l /dev/hda >> $x ; y >> $x........etc


I'm hoping to create a simple script soon to accept any number of "appropriate" files for this - it will be general.


Actually - modifying the simple function a little to :

y() { echo -e "\n\n" >> $x; }


we have:


x=”a” ; y() { echo -e "\n\n" >> $x; } ; cat b > $x ; y ; cat c >> $x ; y ; cat d >> $x ; y...........etc

Skyline 08-29-2003 04:16 PM

Hi guys :

Can anyone point out the correct way to set up an array in this scenario?

I finished off yesterday with a simple line in the shell which enables me to concatenate any number of files in a neat compilation file at the end -

x=”a” ; y() { echo -e "\n\n" >> $x; } ; cat b > $x ; y ; cat c >> $x ; y ; cat d >> $x ; y...........etc

Today - I've written this simple script to mimic the above - it works great - however I dont know the syntax for using an array variable with read - so I'm limited to the number of variables I set up for read - as an example I ve just used 3.

Code:

#!bin/bash

y() { echo -e "\n\n" >> $compfile; }
echo - "Please type the path to the compilation file :"
read compfile
echo "Please type the original files that are going to make up the compilation file :"
read orig1 orig2 orig3
cat $orig1 > $compfile ; y ; cat $orig2 >> $compfile ; y ; cat $orig3 >> $compfile

TIA

Skyline 08-29-2003 08:56 PM

Code:

#!bin/bash

y() { echo -e "\n\n" >> $compfile; }
echo - "Please type the path to the compilation file :"
read compfile
echo "Please type the original files that are going to make up the compilation file :"
read -a orig
cat ${orig[0]} > $compfile
y

Ok - so Ive got the array set up correctly and have redirected the contents of the first element to $compfile.

Now what I want to do is to create a for loop in which every element from the second onwards ie [1]........n gets appended to $compfile, then call y() to put in the blank lines between - The problem is, I dont know the correct syntax and way of setting the for loop up to do this - appreciate any ideas.

TIA


All times are GMT -5. The time now is 01:46 AM.