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
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
Related
I try to search for files and seperate path and version as variable because each will be needed later for creating a directory and to unzip a .jar in desired path.
file=$(find /home/user/Documents/test/ -path *.jar)
version=$(echo "$file" | grep -P -o '[0-9].[0-9].[0-9].[0-9]')
path=$(echo "$file" | sed 's/\(.*\)[/].*/\1/')
newpath=$(echo "${path}/${version}")
echo "$newpath"
result
> /home/user/Documents/test/gb0500
> /home/user/Documents/test/gb0500 /home/user/Documents/test/gb0500
> /home/user/Documents/test /home/user/Documents/test/1.3.2.0
> 1.3.2.1
> 1.3.2.2
> 1.2.0.0
> 1.3.0.0
It's hilarious that it's only working at one line.
what else I tried:
file=$(find /home/v990549/Dokumente/test/ -path *.jar)
version=$(grep -P -o '[0-9].[0-9].[0-9].[0-9]')
path=$(sed 's/\(.*\)[/].*/\1/')
while read $file
do
echo "$path$version"
done
I have no experience in scripting. Thats what I figured out some days ago. I am just practicing and trying to make life easier.
find output:
/home/user/Documents/test/gb0500/gb0500-koetlin-log4j2-web-1.3.2.0-javadoc.jar
/home/user/Documents/test/gb0500/gb0500-koetlin-log4j2-web-1.3.2.1-javadoc.jar
/home/user/Documents/test/gb0500/gb0500-koetlin-log4j2-web-1.3.2.2-javadoc.jar
/home/user/Documents/test/gb0500-co-log4j2-web-1.2.0.0-javadoc.jar
/home/user/Documents/test/gb0500-commons-log4j2-web-1.3.0.0-javadoc.jar
As the both variables version and path are newline-separated, how about:
file=$(find /home/user/Documents/test/ -path *.jar)
version=$(echo "$file" | grep -P -o '[0-9].[0-9].[0-9].[0-9]')
path=$(echo "$file" | sed 's/\(.*\)[/].*/\1/')
paste -d "/" <(echo "$path") <(echo "$version")
Result:
/home/user/Documents/test/gb0500/1.3.2.0
/home/user/Documents/test/gb0500/1.3.2.1
/home/user/Documents/test/gb0500/1.3.2.2
/home/user/Documents/test/1.2.0.0
/home/user/Documents/test/1.3.0.0
BTW I do not recommend to store multiple filenames in a single variable
as a newline-separated variable due to several reasons:
Filenames may contain a newline character.
It is not easy to manipulate the values of each line.
For instance you could simply say
the third line as path=${file%/*} if file contains just one.
Hope this helps.
I want to make UNIX script, which will automatically move my working directory files to newly created directories.
Example: In you dir you got files:
001-file.html,
001-file.rb,
002-file.html,
002-file.rb
And 2 files will be moved to ./NewDir/001-file and another 2 to ./NewDir/002-file
My problem is that after I get correct string from Unix commands I cannot assign it to variable.
Here is my code:
clear
echo "Starting script"
echo "Dir = "$(pwd)
read -p "Please enter count(max '999') of different file groups:" max_i
read -p "Enter new dir name:" outer_dir_name
for ((i=0; i<=$max_i;i++)) do
a1=$(($i/100))
a2=$((($i-$a1*100)/10))
a3=$(($i-($a2*10)-($a1*100)))
inner_dir_name=$((ls *[$a1][$a2][$a3]* 2>/dev/null | head -n 1 | cut -f1 -d"."))
echo $inner_dir_name
echo "--------------"
done
One pair of round parentheses is enough for command substitution.
inner_dir_name=$(ls *[$a1][$a2][$a3]* 2>/dev/null | head -n 1 | cut -f1 -d".")
It looks like you're going about the operation the hard way. I would probably do something like this, assuming that there are no spaces in the file names:
ls | sed 's/\..*$//' | sort -u |
while read prefix
do
mkdir -p $outer_dir_name/$prefix
mv $prefix.* $outer_dir_name/$prefix
done
The ls could be made more precise with:
ls [0-9][0-9][0-9]-file.*
If I was worried about blanks and other odd-ball characters in the file names, I'd have to use something more careful:
for file in [0-9][0-9][0-9]-file.*
do
prefix=${file%%.*}
[ -d "$outer_dir_name/$prefix" ] || mkdir -p "$outer_dir_name/$prefix"
mv "$file" "$outer_dir_name/$prefix"
done
This executes more mv commands, in general.
I have a series of files
484_mexico_201401.dat
484_mexico_201402.dat
484_mexico_201403.dat
… so on
I want to make files
484_mexico_201401.mft which will have below containt
484 | datfile name | line count for the .dat file
Example:
484|484_mexico_201401.dat|6000
can anyone help with a shell script for this ?
You can try bash,
for file in 484_*
do
new_file=${file%.*};
echo "$(sed 's/^\([^_]\+\)_.*/\1/'<<<$file)|$file|$(wc -l $file|cut -d' ' -f1)" > "$new_file.mft";
done
you can also try this.
location="./";for file in $(ls $location);do echo "$(echo $file|cut -d '_' -f 1)|$file|$(wc -l $file | cut -d ' ' -f 1)" >> output.txt;done
Then you will be able to read the new file buy typing cat output.txt
If you want to full script for it, then you may need to add the #!/bin/bash to the first line in the script.
#!/bin/bash
location="./";for file in $(ls $location);do echo "$(echo $file|cut -d '_' -f 1)|$file|$(wc -l $file | cut -d ' ' -f 1)" >> output.txt;done
save that into a file where you want the script to be then run chmod 555 scriptname.sh and you should be able to run.
I'm trying to use the following to locate a file then copy the first result to the directory that is set by $dir. It works fine when I don't set a variable and use an absolute path, but that's not what I need.
This doesn't work:
dir="/path/to/destination/";
mkdir "$dir";
locate -l 1 target_file.txt | awk '{print "cp " $1 $dir " "}' | sh
error message is:
awk: illegal field $(), name "dir"
input record number 1, file
source line number 1
cp: fts_open: No such file or directory
This works:
locate -l 1 target_file.txt | awk '{print "cp " $1 " /path/to/destination/"}' | sh
Inside an awk script, you don't use $ to prefix variables; you use it to refer to fields in the line of input.
dir="/path/to/destination/";
mkdir "$dir";
locate -l 1 target_file.txt | awk -v dir="$dir" '{print "cp", $1, dir}' | sh
This will work OK as long as you have no spaces in your names.
dir="/path/to/destination/";
mkdir "$dir";
locate -l 1 target_file.txt | awk -v dir="$dir" '{printf "cp \"%s\" \"%s\"\n", $1, dir}' | sh
Unless I screwed up the backslashes, that should work with spaces etc in file names. The whole pipeline will be screwed up if someone is unkind enough to put a newline into a file name.
i need some help here as i couldn't figure it out the errors.
read input
echo -e "$input" | cut -c4-
cd "$input" | cut -c4-
So I enter cd test, echo output is correct which is test.
I would like to change directory but it gives cd cd test.
Any help is appreciated.
What you want should be:
cd $(echo -e "$input" | cut -c4-)
Which can be done as well like this (see bash "here strings")
cd $(cut -c4- <<<$input)