UPDATE Edited the script slightly for a more specific regex, still same problem.
Clarifying following questions, this is my exact script I want to get working (POC for a bigger script), test.txt is in the same directory as my test.sh file and I'm running the script cd'ed in the same directory.
Also, whilst tinkering, I think this might be related to carriage returns, but I still can't figure out what's going wrong.
END UPDATE
I've looked around here to paste together a bash file I can run to extract a version number from a specific file, and do stuff with that version number via the variable. I have the rest of the script working if I just supply the version via an argument $1, but it'd be neat if it could just determine the version number directly from the file in the folder I want to manipulate.
Here's my current script
#!/bin/sh version=$(sed -ne "s/\\Version *: *\([0-9a-zA-Z\.\-]+\)*/\1/p" test.txt) printf 'detected version %s for test.txt\n' "$version"
Which returns me this value in terminal
for test.txtion 0.6
It seems to overwrite part of the string I'm trying to print. If I just take this part:
sed -ne "s/\\Version *: *\([0-9a-zA-Z\.\-]+\)*/\1/p" test.txt
It works for example if there's a line in the file containing this line
Version: 0.6
I get this:
root@webserver [/home/username]# sed -ne "s/\\Version *: *\([0-9a-zA-Z\.\-]+\)*/\1/p" test.txt 0.6 root@webserver [/home/username]#
Any ideas on what's wrong for the script?
\r
) in the "Version:..." line? I think the start of your output might be overwritten by the end. Try removingfor $file
to see, if the version number is printed.$file.php
whenfile
is already assigned the valuefilename.php
). Does the filefilename.php.php
exist?grep
instead ofsed
... And do not use variables in the FORMAT argument ofprintf
. Useprintf 'detected version %s for %s\n' "$version" "$file"