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 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]".
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.
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"
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:
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
#!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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.