Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-05-2017, 04:19 AM
|
#1
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Rep: 
|
Customising output lists from a script...
..to give a clue as to what list (generic or particular) the compared data came from.
bash
Let's say I had a list of images from National Geographic. I'd likely name the list file natgeo.txt or just natgeo. If I used that list in a script to find dimensions, resolution, or particular fields of metadata, rather than the more general "list.txt," I should want to know the difference between the many, or be able to find just the pictures from NatGeo in one instead of visually sorting out the other list.
Some time ago, before my (to me) unexpected and unwelcome break from Internet time, I had scripts that would take part of the names of original lists and write to lists that had those "fragments" in them. The closest I can come to it now is:
Code:
#!/bin/bash
IFS=$(echo -en "\n\b")
meat=0; dairy=0
function findtag () {
while read file0
do
echo -e "Checking file #$((dairy+1)), $file0"
tref1=$(exiftool -fast5 -q -s -S -OriginalTransmissionReference "$file0" 2>/dev/null)
if [[ -n "$tref1" ]];then
echo -e "$file0 \e[42mHAS\e[0m a Transmission Reference"
echo "Writing to file."
echo -e "$file0:$tref1">> $callit-hastref.txt
((meat++))
fi
if [[ -z "$tref1" ]]; then
#if -n "${tref1/[ ]*\n/}"; then
echo -e "$file0 does \e[41mNOT\e[0m have a Transmission Reference."
echo "Writing to file."
echo "$file0">>"$callitnotref.txt"
fi
dairy=$((dairy+1))
done<"$capfile1"
echo -e "$meat out of $dairy files had Transmission References."
}
function gettext () {
echo -e "What list will I be using?"
read -e -r thelist
#Allowing for, and correcting, the trailing space in interactive mode
if [[ "$thelist" =~ " " ]]; then
capfile1=${thelist% *}
else
capfile1=$thelist
fi
callit=$(echo $thefile | sed -e 's/\..*$//')
echo $callit
exit 0
findtag
}
gettext
IFS=$SAVEIFS
and it doesn't work.
I need another pair of eyes. I think
Carver
|
|
|
02-05-2017, 05:24 AM
|
#2
|
LQ Guru
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
|
What doesn't the script do that you want it to do? A few notes are nice. You can use awk to easily parse the EXIF output and redirect it to the list file. In the sed command I'm not sure what \.. is supposed to do. But if it's to remove 2 periods, then \.\. might be better. It's also easier to read if you use semicolons for delimiters in sed.
To debug shell scripts I comment everything out and put in echo commands to indicate where I am in the script. Then uncomment one section at a time and run it each time. One trick is to use tr to make multiple line outputs into a single line with space delimiters and then use awk to parse the desired parts.
|
|
|
02-05-2017, 10:35 AM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031
|
So the main problem we as outsiders have is that we have no access to any of your pictures, the original files you are dealing with or any other information that would help to deal
with solving your script. You have provided a script but with no other details, perhaps error messages or information on what has gone wrong / is not happening, it would seem that we will not be able to assist.
As pointed out by AwesomeMachine, another big issue is that there is 1, I repeat, 1 comment in the entire script. How is anybody reading this going to help if we have no idea whatsoever what it is
you are actually trying to achieve within the script?
So you will need to supply data from both before and after the script has run and provide details what the script is doing and based on your own extensive testing so far, where you think it might be
going wrong?
|
|
|
02-05-2017, 11:05 AM
|
#4
|
Member
Registered: May 2016
Posts: 222
Rep: 
|
Code:
function findtag () {
...
}
function gettext () {
...
echo "$callit"
exit 0
findtag
}
gettext
You sure you want the exit 0 before findtag?
In the function gettext you read thelist, but never seem to use it (besides removing trailing space, in case it is there).
Code:
callit=$(echo $thefile | sed -e 's/\..*$//')
thefile is only to be found once, and that is here. typo?
callit is only used twice, not sure if it makes any sense to store it in a variable at all.
-
I got hard times to understand the code (might well be me). The above are only questions.
|
|
|
02-05-2017, 01:05 PM
|
#5
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,685
|
probably it was already suggested, but I will repeat: please use shellcheck or similar tool to check your script. Actually I tried now https://www.shellcheck.net/
Code:
Line 7:
while read file0
^-- SC2162: read without -r will mangle backslashes.
Line 14:
echo -e "$file0:$tref1">> $callit-hastref.txt
^-- SC2086: Double quote to prevent globbing and word splitting.
Line 22:
echo "$file0">>"$callitnotref.txt"
^-- SC2154: callitnotref is referenced but not assigned.
Line 37:
callit=$(echo $thefile | sed -e 's/\..*$//')
^-- SC2001: See if you can use ${variable//search/replace} instead.
^-- SC2154: thefile is referenced but not assigned.
^-- SC2086: Double quote to prevent globbing and word splitting.
Line 38:
echo $callit
^-- SC2086: Double quote to prevent globbing and word splitting.
|
|
2 members found this post helpful.
|
02-05-2017, 02:19 PM
|
#6
|
LQ Guru
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
|
Quote:
Originally Posted by pan64
probably it was already suggested, but I will repeat: please use shellcheck or similar tool to check your script. Actually I tried now [url]https://www.shellcheck.net/
|
I'd never heard of this. Looks extremely useful. Thanks for the heads-up, pan64.
|
|
|
02-06-2017, 01:24 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,685
|
Quote:
Originally Posted by hydrurga
I'd never heard of this. Looks extremely useful. Thanks for the heads-up, pan64.
|
you are welcome. You can also download it and use offline - and also there can be an installable package named shellcheck
Last edited by pan64; 02-06-2017 at 02:19 AM.
|
|
|
02-13-2017, 11:55 AM
|
#8
|
Member
Registered: Sep 2016
Location: Webster MA USA
Posts: 243
Original Poster
Rep: 
|
Le's say I have a list file called "empires.txt." I want to have the script dump its yes/no results to an "empires-hastref.txt" file & an "empires-notref.txt" file. I came up with some code that works in stdout (I have yet to test it in the script as of this writing), which looks like
Code:
callit=$(echo $thefile | sed -e 's/\..*$//')
nocall=$(echo -e "$callit-notref.txt")
yescall=$(echo -e "$callit-hastref.txt")
Another thing I want to test is whether or not the sed command callit assigns will work on file names without conventional DOS extensions like .txt.
I'll make an edit of this post as soon as I know whether or not the code works with one-word (i.e., no-extension) file names.
Carver
------------------------------------------------------------------------
Ten Minutes Later...
Guess what -- The code does NOT work. Not even on "empires.txt" (a real file).
returns an empty string. Subsequently the yes/no lists it writes to start with single spaces. I'm sure you folks gave me the reasons this would happen, but maybe I missed them.
I also added a "set -e" higher up in the body of the script (right after the IFS setting), and I suppose it's because the script was in otherwise OK bash that it did not exit when it encountered the empty string assingned to the callit variable.
-Carver
Last edited by L_Carver; 02-13-2017 at 12:04 PM.
Reason: Subsequent developments proved disappointingly accurate.
|
|
|
02-13-2017, 12:00 PM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,685
|
that kind of extension is not important in linux, so it should work the same way with or without any extension. Just take care about the filename, you should use the same name everywhere.
|
|
|
02-13-2017, 12:39 PM
|
#10
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031
|
Not only am I getting lost with which part of your question your up to, but in your latest example what is the point of all the echoes?
Code:
thefile="empires.txt"
callit="${thefile%.*}"
nocall="$callit-notref.txt"
yescall="$callit-hastref.txt"
As stated in a previous thread of yours', the issue that could come about is if you have a file called, "this.is.a.file", which may or may not be an extension you wish to remove but that no code
will no that without you telling it what is an acceptable extension.
|
|
|
All times are GMT -5. The time now is 09:17 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|