LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Simplifying a script (https://www.linuxquestions.org/questions/linux-software-2/simplifying-a-script-4175595195/)

L_Carver 12-10-2016 05:34 PM

Simplifying a script
 
I have a script using Exiftool to remove the software tag, comment header line and date data (in both EXIF and IPTC) from files saved with Gimp 2.8 or 2.9. I want to simplify it so the user needn't go any further than to type the name of the script and the name of the file they want to strip.

The script is called degimp.
Code follows.
Code:

#!/bin/bash -i
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

echo -e "What file do you want to 'De-GIMP'?"
read -e item
file1=$item
if [ -f "$file1" ]; then
        exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $file1
        echo "File $file1 is now De-GIMPed."
        echo "Check it for yourself with any good tool."
else
        echo "Your file is not in this directory."
        echo "No file to de-GIMP."
        exit 0
fi
IFS=$SAVEIFS

I'm still a little sketchy w/re the whole "$1" thing. Would a line at the top like
Code:

file1="$1"
work in place of the current prompt?

Carver

BW-userx 12-10-2016 05:54 PM

Code:

$./FileName argument argument
  $0          $1      $2

build in Bash assignments for variables.

Code:



Removed this line #!/bin/bash -i, <- I do not know what that -i is for,
 but it kept restarting my shell,
so I replaced with this


#!/bin/bash

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

#echo -e "What file do you want to 'De-GIMP'?"
#read -e item

echo "$0"
echo "$1"


#file1="item"


if [ -f "$1" ]; then
#        exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $1
        echo "File $1 is now De-GIMPed."
        echo "Check it for yourself with any good tool."
else
        echo "Your file is not in this directory."

        echo "No file to de-GIMP."
        exit 0
fi
IFS=$SAVEIFS

just type in the script name and then file name hit enter.


Code:

userx@voided1.what~>> ./xtest textCattwo
./xtest
textCattwo
File textCattwo is now De-GIMPed.
Check it for yourself with any good tool.


c0wb0y 12-10-2016 06:23 PM

Code:

#!/bin/bash

[[ -f $1 ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $$(date +%F).$1 && echo -e "File $file1 is now De-GIMPed.\nCheck it for yourself with any good tool."; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."


BW-userx 12-10-2016 06:31 PM

if you want to do more error checking, say for the proper file type.

Code:

#!/bin/bash


SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

echo "$0"
echo "$1"

                c=$1
       
                ext=${c##*.}
                echo "ext $ext"
               
               

if [[ -f "$1" && "$ext" == 'mp4' || "$ext" == 'mkv' ]]; then


#        exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $file1
        echo "File $1 is now De-GIMPed."
        echo "Check it for yourself with any good tool."
else
        echo "Your file is not in this directory."
        echo "No file to de-GIMP."
       

      exit 1  <- error is not exit 0
fi
#IFS=$SAVEIFS

if it is the wrong extension then it gets error'ed out.

Code:

userx@voided1.what~>> ./xtest textCattwo.oop
./xtest
textCattwo.oop
ext oop
Your file is not in this directory.
No file to de-GIMP.


note to @c0wb0y

make that one a one liner. :D

BW-userx 12-10-2016 06:57 PM

@c0wb0y

I had to fix your code, I just added mine to yours before checking yours.

Code:

userx@voided1.what~>> ./xtest textCattwo.mkv
./xtest
textCattwo.mkv
ext mkv
./xtest: line 20: syntax error near unexpected token `('
./xtest: line 20: `[[ -f $1  && "$ext" == 'mp4' || "$ext" == 'mkv' ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $$(date +%F).$1 && echo -e "File $file1 is now De-GIMPed.\n"Check it for yourself with any good tool.""; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."'

Code:

#!/bin/bash

#-i
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

c=$1
ext=${c##*.}
echo "ext $ext"
               

[[ -f $1  && "$ext" == 'mp4' || "$ext" == 'mkv' ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $(date +%F).$1 && echo -e "File $file1 is now De-GIMPed.\n"Check it for yourself with any good tool.""; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."


IFS=$SAVEIFS


c0wb0y 12-10-2016 07:02 PM

I know. Those damn extra double quotes and $ sign. These fat fingers. :)

BW-userx 12-10-2016 07:32 PM

Quote:

Originally Posted by c0wb0y (Post 5640476)
I know. Those damn extra double quotes and $ sign. These fat fingers. :)

I fix that then ran it and it still throws an error, so I fixed that and I am not sure what type of files he is using. but that Date I did not fix. getting this error

Code:

userx@voided1.what~>> ./xtest Pitch.Perfect.2.2015.720p.mp4
ext mp4
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in IFD0:ModifyDate (PrintConvInv)
Error: Truncated mdat atom - Pitch.Perfect.2.2015.720p.mp4
Your file is not in this directory.
No file to de-GIMP.

using this code.

Code:

#!/bin/bash

#-i
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

c=$1
ext=${c##*.}
echo "ext $ext"
               

#[[ -f $1  && "$ext" == 'mp4' || "$ext" == 'mkv' ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $(date +%F).$1 && echo -e "File $file1 is now De-GIMPed.\n"Check it for yourself with any good tool.""; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."


#### this one
[[ -f $1 ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate="$(date +%F)" $1 && echo -e "File $file1 is now De-GIMPed.\n"Check it for yourself with any good tool.""; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."

IFS=$SAVEIFS


Just a date format issue NBD, I changed it to this
Code:

-ModifyDate="$(date +%F%T)" $1  <-- exiftool needs file name last to process it.
removing the filename because it kept looking for the annotated file and not the one added on the command line.

Code:

userx@voided1.what~>> ./xtest Pitch.Perfect.2.2015.720p.mp4
ext mp4
Error: File not found - 2016-12-10.Pitch.Perfect.2.2015.720p.mp4
Error: Truncated mdat atom - Pitch.Perfect.2.2015.720p.mp4
Your file is not in this directory.
No file to de-GIMP.

I do not know what type of file he is actually working with, but just using this line(s)
Code:


#!/bin/bash

#-i
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

c=$1
ext=${c##*.}
echo "ext $ext"
               

[[ -f $1  && "$ext" == 'mp4' || "$ext" == 'mkv' ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate="$(date +%F%T)" $1 && echo -e "File $1 is now De-GIMPed.\n"Check it for yourself with any good tool.""; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."

#[[ -f $1 ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate="$(date +%F%T)" $1 && echo -e "File $file1 is now De-GIMPed.\n"Check it for yourself with any good tool.""; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."

IFS=$SAVEIFS

keeping in that error checking for extensions type with and without it I still get the same error.

Code:

userx@voided1.what~>> ./xtest Pitch.Perfect.2.2015.720p.mp4
ext mp4
Error: Truncated mdat atom - Pitch.Perfect.2.2015.720p.mp4
Your file is not in this directory.
No file to de-GIMP.

so it kicks it out and sends the last message.

now if he, the OP can figure all of this out, that'd be a good thing. :D :D

L_Carver 12-10-2016 07:44 PM

Thanks for all the help and suggestions...
 
After a little more Google-ing and getting friendly with Stack Exchange, I came up with this re-edit of my original script:
Code:

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

file1="$1"

if [ -f "$file1" ]; then
        exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $file1
        echo "File $file1 is now De-GIMPed."
        echo "Check it for yourself with any good tool."
else
        echo ""
        echo "No file to de-GIMP."
        exit 0
fi
IFS=$SAVEIFS

Tested it six times already. No errors (a surprise) and the tags are indeed removed.

I have yet to test the Your file is not in this directory. Which seems unnecessary since the user is naming the file to be de-gimp'ed. I may just edit it out. That sort of thing fits better with a "while read line" kind of loop, which was what I was trying to get away from.

I kept the original script in my execution path, of course and renamed it degimp2. degimp seems more appropriate for the one where the filename follows the command to start the script. I was surprised to not get any bash error messages. I guess the changes I made were up to spec for bash 4.3.

Carver

c0wb0y 12-10-2016 07:56 PM

Hah! Problem with me writing on-the-fly and not reading what those arguments for.

Code:

#!/bin/bash

[[ -f $1 ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $1 && echo -e "File $file1 is now De-GIMPed.\nCheck it for yourself with any good tool."; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."


BW-userx 12-10-2016 07:59 PM

Quote:

Originally Posted by c0wb0y (Post 5640491)
Hah! Problem with me writing on-the-fly and not reading what those arguments for.

Code:

#!/bin/bash

[[ -f $1 ]] && { exiftool -fast5 -overwrite_original_in_place -P -q -Software= -Comment= -IPTC:DateCreated= -IPTC:TimeCreated= -ModifyDate= $1 && echo -e "File $file1 is now De-GIMPed.\nCheck it for yourself with any good tool."; } ||  echo -e "Your file is not in this directory.\nNo file to de-GIMP."


I think he wanted to make everything void and null .. anyways, he went to another store to get what he needed after asking us.

Fat_Elvis 12-11-2016 03:20 PM

I like Cowboy's script the best but why oh why is it all in one line?

To save space?

BW-userx 12-11-2016 03:30 PM

Quote:

Originally Posted by Fat_Elvis (Post 5640756)
I like Cowboy's script the best but why oh why is it all in one line?

To save space?

that is the thing about coding, you can one line it. you still need to declare /bin/bash and you output, I just added error checking file types.

Fat_Elvis 12-11-2016 04:15 PM

Quote:

Originally Posted by BW-userx (Post 5640759)
that is the thing about coding, you can one line it. you still need to declare /bin/bash and you output, I just added error checking file types.

I understand the script, just found his choice to "one line" it funny. Which was probably the intention.

BW-userx 12-11-2016 05:03 PM

Quote:

Originally Posted by Fat_Elvis (Post 5640773)
I understand the script, just found his choice to "one line" it funny. Which was probably the intention.

it is just a coding style. if you can do it in one line rather then many lines. well then what is to be said of that?

Fat_Elvis 12-11-2016 05:27 PM

Quote:

Originally Posted by BW-userx (Post 5640789)
it is just a coding style. if you can do it in one line rather then many lines. well then what is to be said of that?

Didn't mean to be critical, just humorous.

Eh, and I'd need one of those super wide screens to fit that in one line in a text editor. Hehe.


All times are GMT -5. The time now is 07:36 PM.