Question

I have a little shell script which goes to a specified Folder and extracts the .tar files in that Folder.

Now,I have to use a parameter to change the path to the Folder. My first line in my script is cd /bla/bla/bla . I want to read the /bla/bla/bla part from a separate file. In that file the first line is PATH= /bla/bla/bla.

Anyone got a suggestion for me?

I implemented the solution from Merlin but the script goes into a loop.

My code is:

cd head -n 1 PARAM.TXT | awk -F'=' '{print $2}' | xargs ./pctrl_ExtractAndRemoveTar.sh;
for file in *.tar.gz; 
  do tmp=${file:34} && b=${tmp%.tar.gz*} && tar tfz "${file}" > "${b}.fl" && tar xzvf "${file}" && rm "${file}"; 
done

Big thanks,

Tom

Was it helpful?

Solution

Here is how you would use a parameter to your script, which I will call Foo.sh.

#!/bin/bash

pushd "$1" 
... Do rest of stuff
popd

When you invoke the script, you would say :

./Foo.sh my/path/goes/here

If you wanted to pull the parameter out of a file, let's say called Bar.txt, you could use head with xargs.

head -n 1 Bar.txt | xargs ./Foo.sh

Note that the line above assumes that the first line of Bar.txt is exactly equal to the path. If you had the form of varname=the/path, you could grab the right part of the assignment using awk.

head -n 1 Bar.txt | awk -F'=' '{print $2}' | xargs ./Foo.sh

Update: It appears that the OP is calling his own script in an infinite loop, if I am reading his code correctly. Since you seem to want to hardcode PARAM.txt into your script, here is probably what you want.

cd `head -n 1 PARAM.TXT | awk -F'=' '{print $2}'`
for file in *.tar.gz; 
  do tmp=${file:34} && b=${tmp%.tar.gz*} && tar tfz "${file}" > "${b}.fl" && tar xzvf "${file}" && rm "${file}"; 
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top