LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-25-2018, 08:48 AM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Bash script help on syntax


Hi Folks, Yet another very simple bash script syntax bit of help needed.

Before I ask just to let you know I've been programming since 1970, BASIC, Fortran, JCL, ADABAS/Natural, PRO-IV, Javascript, HTML, Coldfusion, SQL and now I'm using bash.

It's got great syntax, nice and concise, and i follow the principles, but some of the probably more obvious sides of it escape me - old age??? maybe, maybe not.

Any way I have folder full of mp4 files.
Code:
onk@XEON4 ~/TEST/700k $ ls -al
total 1777376
drwxrwxr-x 2 onk onk     32768 Nov 22 23:25 .
drwxrwxr-x 7 onk onk    430080 Nov 23 14:50 ..
-rw-rw-r-- 1 onk onk   5680963 Jun 23 19:15 10013.mp4
-rw-rw-r-- 1 onk onk   4640217 Jun 23 19:16 10125.mp4
-rw-rw-r-- 1 onk onk   5866973 Jun 23 19:17 10323.mp4
I have a bash executable that displays WxH

Code:
onk@XEON4 ~/TEST/700k $ VWxH.sh 10013.mp4
512x384


onk@XEON4 ~/TEST/700k $ for i in *.mp4; do echo $i VWxH.sh $i ;done
10013.mp4 VWxH.sh 10013.mp4
10125.mp4 VWxH.sh 10125.mp4
10323.mp4 VWxH.sh 10323.mp4
How do I get it to display the mp4's WxH in a for loop?

I guess I could update VHxH.sh to the filename as well as the video dimensions but I really want to know how to wriet the for loop.

This is VWxH.SH
Code:
#!/bin/bash
exiftool -Imagesize -s3 $1
Cheers

Last edited by GPGAgent; 11-25-2018 at 08:50 AM. Reason: added a bit more
 
Old 11-25-2018, 09:09 AM   #2
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
Missing a semi-coma :
Code:
for i in *.mp4; do echo $i; VWxH.sh $i ;done
 
Old 11-25-2018, 09:26 AM   #3
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
Just a tip, maybe.
When you are writing loop in a terminal, you can start it :
Code:
for i in *.mp4; do
Hit <enter>, it will show you did not end your command by adding '>' and continue :
Code:
> echo $i
Hit <enter> again :
Code:
> VWxH.sh $i
Hit <enter> again :
Code:
> done
Hit <enter>, and then, as this is the end of your loop, it will run it.
 
Old 11-25-2018, 10:48 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
In general:
if you want to check how your script actually working: add set -xv at the beginning of your script
if you wish to check the syntax of your script use shellcheck (www.shellcheck.net).
 
1 members found this post helpful.
Old 11-25-2018, 12:24 PM   #5
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by lougavulin View Post
Missing a semi-coma :
Code:
for i in *.mp4; do echo $i; VWxH.sh $i ;done
Thanks for that, it's close, but not close enough.
Code:
onk@XEON4 ~/TEST/700k $ for i in *.mp4; do echo $i; VWxH.sh $i ;done
10013.mp4
512x384
10125.mp4
384x288
10323.mp4
512x384
10595.mp4
512x384
I don't wnat a cr/fl after the mp4 name, I want this:

Code:
10013.mp4  512x384
10125.mp4  384x288
10323.mp4  512x384
10595.mp4  512x384
And then I would like to rename the file to this:
Code:
10013-512x384.mp4
10125-384x288.mp4
10323-512x384.mp4
10595-512x384.mp4
cheers
 
Old 11-25-2018, 12:25 PM   #6
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by pan64 View Post
In general:
if you want to check how your script actually working: add set -xv at the beginning of your script
if you wish to check the syntax of your script use shellcheck (www.shellcheck.net).
Great tip, I'll try it out. Thanks
 
Old 11-25-2018, 12:29 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
see man echo
Code:
-n     do not output the trailing newline
 
Old 11-25-2018, 12:44 PM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
echo is a bash built-in I think. but anyway, echo -n works. Also you can use printf.

Last edited by pan64; 11-25-2018 at 12:46 PM.
 
Old 11-25-2018, 12:46 PM   #9
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by scasey View Post
see man echo
Code:
-n     do not output the trailing newline
Cheers, man echo should have been my friend

Code:
onk@XEON4 ~/TEST/700k $ for i in *.mp4; do echo -n $i "  " ; VWxH.sh $i ;done
10013.mp4   512x384
10125.mp4   384x288
10323.mp4   512x384
 
Old 11-25-2018, 12:51 PM   #10
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
So final challenge, using this script so far I would like to mv the fule to a new name w=that includes the WxH

Code:
for i in *.mp4; do echo -n $i "  " ; VWxH.sh $i ;done
New file names
Code:
10013-512x384.mp4   
10125-384x288.mp4   
10323-512x384.mp4   
   or
512x384-10013.mp4   
384x288-10125.mp4   
512x384-10323.mp4
 
Old 11-25-2018, 01:10 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
use something like this inside the for loop:
Code:
s=$(VWxH.sh $i)
mv ${i%.mp4} $s-$i
 
Old 11-25-2018, 05:13 PM   #12
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by pan64 View Post
use something like this inside the for loop:
Code:
s=$(VWxH.sh $i)
mv ${i%.mp4} $s-$i
Thanks, just what I've been trying but the syntax is getting to me
Code:
onk@XEON4 ~/STUFF/VIDS/TEST $ for i in *.mp4; do echo -n $i "  " ; VWxH.sh $i ;s=$(VWxH.sh $i);mv ${i%.mp4} $s-$i;  done
1467021_hq.mp4   600x480
mv: cannot stat '1467021_hq': No such file or directory
1684525_hq.mp4   720x480
mv: cannot stat '1684525_hq': No such file or directory
1696132_hq.mp4   1280x720
mv: cannot stat '1696132_hq': No such file or directory
18606_hq.mp4   1280x720
mv: cannot stat '18606_hq': No such file or directory
19586_hq.mp4   640x480
mv: cannot stat '19586_hq': No such file or directory
onk@XEON4 ~/STUFF/VIDS/TEST $ ls -al
total 248244
drwxrwxr-x  2 onk onk     4096 Nov 25 14:55 .
drwxrwxr-x 10 onk onk   323584 Nov 25 18:56 ..
-rw-rw-r--  1 onk onk 25950908 Sep 30 18:49 1467021_hq.mp4
-rw-rw-r--  1 onk onk 52623483 Sep 30 19:20 1684525_hq.mp4
-rw-rw-r--  1 onk onk 88912589 Sep 30 19:18 1696132_hq.mp4
-rw-rw-r--  1 onk onk 72904313 Sep 30 18:54 18606_hq.mp4
-rw-rw-r--  1 onk onk 13403523 Sep 30 17:27 19586_hq.mp4
onk@XEON4 ~/STUFF/VIDS/TEST $ 
onk@XEON4 ~/STUFF/VIDS/TEST $ for i in *.mp4; do echo -n $i "  " ; VWxH.sh $i ;  done
1467021_hq.mp4   600x480
1684525_hq.mp4   720x480
1696132_hq.mp4   1280x720
18606_hq.mp4   1280x720
19586_hq.mp4   640x480
onk@XEON4 ~/STUFF/VIDS/TEST $

Last edited by GPGAgent; 11-25-2018 at 05:15 PM.
 
Old 11-25-2018, 05:34 PM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,696

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Code:
${i%.mp4}
Basically that strips .mp4 from your filename which is not what you want. Instead try

Code:
mv $i $s-$i
This might make it easier.
Code:
#!/bin/bash

for i in *.mp4; do 
    s=$(VWxH.sh $i)
    echo "$i $s"
    mv $i $s-$i
done
I assume you are using a test directory of your files just in case?
 
Old 11-26-2018, 01:12 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you might want to use ", like this:
Code:
mv "${i%.mp4}" "${s}-${i}"
also please try set -xv as it was mentioned, you will see what's happening (and probably post the result and we will explain)
also you can add echo before mv to check the script (and remove echo if that was ok)
 
1 members found this post helpful.
Old 11-26-2018, 08:22 AM   #15
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Thanks pan64 and michaelk, those tips helped

Code:
onk@XEON4 ~/STUFF/VIDS/TEST $ for i in *.mp4; do echo -n $i "  " ; VWxH.sh $i ;cp $i $(VWxH.sh $i)-$i;  done
1467021_hq.mp4   600x480
1684525_hq.mp4   720x480
1696132_hq.mp4   1280x720
18606_hq.mp4   1280x720
19586_hq.mp4   640x480
onk@XEON4 ~/STUFF/VIDS/TEST $ for i in *.mp4; do echo -n $i "  " ; VWxH.sh $i ;cp $i $i-$(VWxH.sh $i);  done
1280x720-1696132_hq.mp4   1280x720
1280x720-18606_hq.mp4   1280x720
1467021_hq.mp4   600x480
1684525_hq.mp4   720x480
1696132_hq.mp4   1280x720
18606_hq.mp4   1280x720
19586_hq.mp4   640x480
600x480-1467021_hq.mp4   600x480
640x480-19586_hq.mp4   640x480
720x480-1684525_hq.mp4   720x480
onk@XEON4 ~/STUFF/VIDS/TEST $
I used cp and not mv, just in case, and it's all good now

The options in the final script will allow for moving or copying, in the same folder or to another folder, and if you precede or follow with the video size.

Many thanks folks

Now solved and I'll be using set -xv as well :-)
 
  


Reply



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
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
[SOLVED] "Error: syntax before '@' token and Error: syntax at 'OTHER' token" bullrider Programming 2 07-27-2009 08:00 AM
Starting httpd: httpd: Syntax error on line 209 of /etc/httpd/conf/httpd.conf: Syntax sethukpathi Linux - Networking 6 04-12-2008 11:26 AM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 06:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:33 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