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 |
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] |
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] |
Quote:
Code:
TST=$(which frbv.0.85) 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]". |
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 |
We posted at exactly the same time.
See my previous post, it solves your problem. |
thanks for your advice!
:-D -=MaGo=- |
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 |
echo -e "\n\n" >> file
will insert two blank lines |
Cheers Strike - that's just what I'm looking for - nice one!
|
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" |
Suppose you had a file e that only had two empty lines.
cat a e b e > c This might clear things up slightly. |
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 |
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 |
Code:
#!bin/bash 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. |