LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 02-05-2017, 04:19 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Rep: Reputation: Disabled
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
 
Old 02-05-2017, 05:24 AM   #2
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
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.
 
Old 02-05-2017, 10:35 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031

Rep: Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202
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?
 
Old 02-05-2017, 11:05 AM   #4
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
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.
 
Old 02-05-2017, 01:05 PM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,685

Rep: Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820
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.
Old 02-05-2017, 02:19 PM   #6
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926Reputation: 2926
Quote:
Originally Posted by pan64 View Post
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.
 
Old 02-06-2017, 01:24 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,685

Rep: Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820
Quote:
Originally Posted by hydrurga View Post
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.
 
Old 02-13-2017, 11:55 AM   #8
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 243

Original Poster
Rep: Reputation: Disabled
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).

Code:
echo $callit
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.
 
Old 02-13-2017, 12:00 PM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,685

Rep: Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820Reputation: 7820
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.
 
Old 02-13-2017, 12:39 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031

Rep: Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202Reputation: 3202
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 08:43 AM
Perl script to suppress matches from two text lists holyearth Linux - General 1 12-23-2010 11:56 PM
[SOLVED] script to exclude words from a lists packets Programming 5 06-16-2010 10:40 PM
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 02:50 PM
Script that lists #of permissions nabil Linux - General 6 04-23-2002 04:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration