LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2003, 02:52 AM   #1
-=MaGo=-
Member
 
Registered: Jun 2003
Location: Italy
Distribution: mandrake; debian
Posts: 33

Rep: Reputation: 15
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

any advice?

thanks

Diego
 
Old 08-28-2003, 05:29 AM   #2
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
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.

Last edited by vanquisher; 08-28-2003 at 05:31 AM.
 
Old 08-28-2003, 05:30 AM   #3
vanquisher
Member
 
Registered: Aug 2003
Location: Hyderabad, India
Posts: 126

Rep: Reputation: 15
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.
 
Old 08-28-2003, 07:37 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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]".

Last edited by Hko; 08-28-2003 at 11:29 AM.
 
Old 08-28-2003, 07:37 AM   #5
-=MaGo=-
Member
 
Registered: Jun 2003
Location: Italy
Distribution: mandrake; debian
Posts: 33

Original Poster
Rep: Reputation: 15
I will try to be more clear

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
 
Old 08-28-2003, 07:59 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
We posted at exactly the same time.
See my previous post, it solves your problem.
 
Old 08-28-2003, 11:06 AM   #7
-=MaGo=-
Member
 
Registered: Jun 2003
Location: Italy
Distribution: mandrake; debian
Posts: 33

Original Poster
Rep: Reputation: 15
thanks for your advice!

:-D

-=MaGo=-
 
Old 08-28-2003, 03:25 PM   #8
Skyline
Senior Member
 
Registered: Jun 2003
Distribution: Debian/other
Posts: 2,104

Rep: Reputation: 45
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

Last edited by Skyline; 08-28-2003 at 03:27 PM.
 
Old 08-28-2003, 03:32 PM   #9
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
echo -e "\n\n" >> file

will insert two blank lines
 
Old 08-28-2003, 03:36 PM   #10
Skyline
Senior Member
 
Registered: Jun 2003
Distribution: Debian/other
Posts: 2,104

Rep: Reputation: 45
Smile

Cheers Strike - that's just what I'm looking for - nice one!
 
Old 08-28-2003, 05:57 PM   #11
Skyline
Senior Member
 
Registered: Jun 2003
Distribution: Debian/other
Posts: 2,104

Rep: Reputation: 45
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"

Last edited by Skyline; 08-28-2003 at 05:59 PM.
 
Old 08-28-2003, 11:18 PM   #12
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
Suppose you had a file e that only had two empty lines.


cat a e b e > c

This might clear things up slightly.
 
Old 08-29-2003, 12:36 AM   #13
Skyline
Senior Member
 
Registered: Jun 2003
Distribution: Debian/other
Posts: 2,104

Rep: Reputation: 45
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

Last edited by Skyline; 08-29-2003 at 03:41 AM.
 
Old 08-29-2003, 04:16 PM   #14
Skyline
Senior Member
 
Registered: Jun 2003
Distribution: Debian/other
Posts: 2,104

Rep: Reputation: 45
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
 
Old 08-29-2003, 08:56 PM   #15
Skyline
Senior Member
 
Registered: Jun 2003
Distribution: Debian/other
Posts: 2,104

Rep: Reputation: 45
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration