Using variables from a text file in Scripting

I would love to make use of a command line program (especially geotifcp) which takes file_a. tif and also file_a. tfw and also results file_b. tif. Primarily it incorporates 2 documents of the very same name with various documents expansions and also oututs the new documents. I would certainly such as to identify just how to set out a couple of hundred of these points. I've developed checklists of all these documents making use of standard ls > output commands.¢ I'm not always seeking a solution targeted at this program yet even more at scripting in general.¢ I'm additionally not seeking you to write this for me, simply offer me an insert the basic instructions please.

Many thanks for any kind of aid!

1
2022-06-07 14:34:55
Source Share
Answers: 3

Command line debates

You possibly what to make use of command line debates.

Attempt this celebration manuscript:

#!/bin/bash

echo "First argument is $1"
echo "Second argument is $2"

If the manuscript documents name is test1.sh, after that you'll have this:

[email protected]:~$ chmod +x test1.sh 
[email protected]:~$ ./test1.sh 
First argument is 
Second argument is 
[email protected]:~$ ./test1.sh file1.tif file2.tiw
First argument is file1.tif
Second argument is file2.tiw

Reading lines from files

Another part would certainly be to loop via your documents. You can do it similar to this:

cat test |while read line; do 
  echo "${line}"
done

Instead of echo "${line}" you can, as an example, do ./test1.sh ${line}.tif ${line}.tiw

1
2022-06-07 14:56:41
Source

I recommend you look at

sed # to manipulate text output from, for instance, ls

and also

xargs # to use some std input to generate (many) new command lines

in mix with piping the result of one command right into the input of the next (like from ls to sed to xargs)

To wit, claim, you have a directory site with *tif and also *tfw documents. You intend to do something to the tif files making use of the equivalent tfw documents. Allow is additionally think for every single tif documents there is a tfw documents of the specific very same name

# list all those tif files in one long column
ls -1 *.tif 

# chop of the end .*tif leaving you with the base name
ls *.tif | sed -s 's/\.tif$//'

# feed the previous into xargs to tag a tif with the info in a tfw and
# create new geo_*.tif file
ls *.tif | sed -s 's/\.tif$//' | xargs -i -t geotifcp -e {}.tfw {}.tif geo_{}.tif

The last line could virtually do what you require in one lengthy (illegible) commandline without scripting.

To assist recognizing you can additionally first create the documents checklist as you did:

ls *tif> output.list

Then tidy it up with sed to see to it it acts well.

sed -s 's/\.tif$//' output.list > clean_output.list

end after that make use of xargs with alternative - p as opposed to - t to get a punctual prior to the issuance of each command:

cat clean_output.list | xargs -i -p geotifcp -e {}.tfw {}.tif geo_{}.tif

EDIT :. A variant on the previous is to make use of even more sed much less xargs. This is additionally a little bit extra 'debuggable'. It does need you to find out a little bit concerning normal expressions (which is well worth the initiative).

Basically, one can create the message of your wanted command totally with sed

sed -s 's/\(.*\)\.tif$/geotifcp -e \1.tfw \1.tif geo_\1.tif/'  output.list > process.sh

I recognize, regex looks dreadful yet below is the harsh description of the web content in the solitary quotes:. 's/ A/B/' replaces A with B A in our instance is (something). tif (note that we need to run away the brackets) The something is saved in bkslsh1 and also obtains made use of to construct the geotifcp command (which is B).

1
2022-06-07 14:56:39
Source

I often tend to do things similar to this making use of sed or awk to construct command lines, after that pipeline them to a shell. As a feasible instance, if you have a documents of simply the xxxxx_a. tif filenames,

sed -e 's/\(.*\)_.*/geotifcp \1_a.tif \1_a.tfw \1_b.tif/' < list-file.txt

to see if it is creating the commands you desire, after that

sed -e 's/\(.*\)_.*/geotifcp \1_a.tif \1_a.tfw \1_b.tif/' < list-file.txt | sh

to run them all.

An input line of asdfasdfsadf_a.tif causes geotifcp asdfasdfsadf_a.tif asdfasdfsadf_a.tfw asdfasdfsadf_b.tif, thinking that the input filenames are [filename]_a.tif and also [filename]_a.tiw and also the result name [filename]_b.tif.

1
2022-06-07 14:54:48
Source