LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2018, 06:53 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
An assignment problem.


Hi:
Code:
for f in CD\ ??/*; do
  echo ${f:49:2}
done
This lists, for every filename, the substring consisting of the 49th and 50th characters of the filename. Now I want to assign the substring to a variable and so I do
Code:
for f in CD\ ??/*; do
  nr = ${f:49:2}
  echo $nr
done
However I get 'Command not found' in line 2. How do I assign the substring to nr?

The directory where I launch the script is this:
Code:
bill@darkstar/almacen/Nonsoft/musica/mozart/Mozart - Complete Piano Concertos$ v -R|more
.:
total 64K
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 01
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 02
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 03
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 04
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 05
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 06
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 07
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 08
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 09
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:28 CD 10
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:10 Covers & Booklet
drwxr-xr-x 2 bill bill 4.0K Dec  7 11:09 Logs
-rwxr-xr-x 1 bill bill   67 Dec  7 21:37 PLAY01
-rwxr-xr-x 1 bill bill   56 Dec  7 21:48 PLAY02
-rwxr-xr-x 1 bill bill   56 Dec  7 21:46 PLAY02~
-rw-r--r-- 1 bill bill   47 Dec  7 11:09 Torrent downloaded from Demonoid.com.txt

./CD 01:
total 256M
-rw-r--r-- 1 bill bill 35M Dec  7 11:27 Mozart, Wolfgang - 01 - Piano Concerto No. 05 in D major, K175 - I. Allegro.flac
-rw-r--r-- 1 bill bill 32M Dec  7 11:26 Mozart, Wolfgang - 02 - Piano Concerto No. 05 in D major, K175 - II. Andante ma un poco adagio.
flac
-rw-r--r-- 1 bill bill 22M Dec  7 11:25 Mozart, Wolfgang - 03 - Piano Concerto No. 05 in D major, K175 - III. Allegro.flac
-rw-r--r-- 1 bill bill 27M Dec  7 11:27 Mozart, Wolfgang - 04 - Piano Concerto No. 06 in B-flat major, K238 - I. Allegro aperto.flac
-rw-r--r-- 1 bill bill 22M Dec  7 11:27 Mozart, Wolfgang - 05 - Piano Concerto No. 06 in B-flat major, K238 - II. Andante un poco adagi
o.flac
-rw-r--r-- 1 bill bill 29M Dec  7 11:27 Mozart, Wolfgang - 06 - Piano Concerto No. 06 in B-flat major, K238 - III. Rondeau. Allegro.fla
c
-rw-r--r-- 1 bill bill 35M Dec  7 11:28 Mozart, Wolfgang - 07 - Piano Concerto No. 07 (for three pianos) in F major, K242 - I. Allegro.
flac
-rw-r--r-- 1 bill bill 35M Dec  7 11:27 Mozart, Wolfgang - 08 - Piano Concerto No. 07 (for three pianos) in F major, K242 - II. Adagio.
flac
-rw-r--r-- 1 bill bill 23M Dec  7 11:27 Mozart, Wolfgang - 09 - Piano Concerto No. 07 (for three pianos) in F major, K242 - III. Rondea
u. Tempo di menuetto.flac

./CD 02:
total 310M
bill@darkstar/almacen/Nonsoft/musica/mozart/Mozart - Complete Piano Concertos$
 
Old 12-07-2018, 07:05 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
no spaces between =
Code:
var=value
NOT
var = value
 
2 members found this post helpful.
Old 12-08-2018, 08:57 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Given the directory structure in post #1 the program should play (mplayer) files including the string 'concerto no. 27' which are one directory level below that in which the program will be run. I came up with
Code:
### PROG APPL: this script run in directory /almacen/Nonsoft/musica/mozart/Mozart - Complete Piano Concertos
###   will play concert #27 (all movements).



for f in CD\ ??/*; do
  nr=${f:49:2}			#Pick up the concert number
  na=${f:30:5}           	#There also are rondos. I only want piano concertos
#  echo $nr
#  echo $na
  if [ "$na" = "Piano" ]; then
    if [ "$nr" = "27" ]; then




    echo $f   
    mplayer "$f"


    fi
  fi
done
which is working fine. All I need now is to replace "27" by $1 and I'll obviate the work of searching all directories (the CD/ ??) to find the concert I want to here (the uploader placed each track (movement) in a separate file!) thanks to your post.
 
Old 12-08-2018, 10:45 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by stf92 View Post
Given the directory structure in post #1 the program should play (mplayer) files including the string 'concerto no. 27' which are one directory level below that in which the program will be run. I came up with
Code:
### PROG APPL: this script run in directory /almacen/Nonsoft/musica/mozart/Mozart - Complete Piano Concertos
###   will play concert #27 (all movements).



for f in CD\ ??/*; do
  nr=${f:49:2}			#Pick up the concert number
  na=${f:30:5}           	#There also are rondos. I only want piano concertos
#  echo $nr
#  echo $na
  if [ "$na" = "Piano" ]; then
    if [ "$nr" = "27" ]; then




    echo $f   
    mplayer "$f"


    fi
  fi
done
which is working fine. All I need now is to replace "27" by $1 and I'll obviate the work of searching all directories (the CD/ ??) to find the concert I want to here (the uploader placed each track (movement) in a separate file!) thanks to your post.
it had nothing to do with my post, if you're referring to me, without mentioning names. then expect everyone to know who you're talking to, so giving deduction a try, it has to be me because I am the only other one that posed in here. all I did was told you the correct way to assigning a value to your variable. just like you asked about.
Quote:
However I get 'Command not found' in line 2. How do I assign the substring to nr?
what your code does is your doing not mine. I take NO credit in it for what it actually does whatsoever. I did not write that code, so I hold no responsibility for what it does.

I do not even know what it does other than chop off your strings, and if they are malformed, then yes, you are going to run into complications.

you are going to have to come up with ways to compensate for the deviation in strings, or come up with a different method to make allowances for the deviations to be there and correct itself to deal with them, either way, I'd come up with a method to try and make them all the same 'pattern' in my strings so all I'd have to do is find that pattern to get what I want and nothing else, so I would not have to go through all of that if it this do this and if it is something else then to this code to match strings to get what I want.

you already have shown you know what you are looking for in the string, now all that needs to be done is make sure that every string adheres to that standard you provided the code to use to get what you are looking for.

as I see you already know you have to put a few more lines of code in there to make sure that you get what you want. you can try sub string matching like this too,
Code:
substring=whatIwant

for or while loop taking in strings

if [[ "string" =~ "$substring" ]] ;
then 

   do something about it.

else
   skip it

fi
--------

substring1=oneThing
substring2=AnotherThing

[[ "$string" =~ "$substring1" ]] && do something on this match 
[[ "$string" =~ "$substring2" ]] && do something on this match
etc..

-----------

  if [ "$na" = "Piano" ]; then
    if [ "$nr" = "27" ]; then
         echo $f   
         mplayer "$f"
     fi
  fi
-------- CAN be this ----------
 if [ "$na" = "Piano" ] && [ "$nr" = "27" ]; then
{
  do something 
}
fi
could be this too,
if [ "$f" =~ "Piano" ] && [ "$f" =~ "27" ]; then
{
   do something
}
fi
but you have to understand how that works too.

Nor do I see how your code in post one could do anything other than assign a value to variable then echo the results, so .. what are you taking about thanks to me your code moved everything into
Quote:
" (the uploader placed each track (movement) in a separate file"thanks to your post.
looks like you already know who's at fault here, the uploader placed each track in a separate file,
thanks to me? I did not even write the code of that program, so stop throwing blame at me or anywhere else but yourself for your mistakes for not knowing what your "uploader" was going to do.

Last edited by BW-userx; 12-08-2018 at 11:46 AM.
 
Old 12-08-2018, 11:44 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks a lot.
 
  


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
Realtek Network Card - IP Assignment Problem qs_tahmeed Solaris / OpenSolaris 13 02-02-2006 01:38 AM
How do I fix this assignment problem C++ compiler error in GCC? Erik_the_Red Programming 4 07-12-2005 09:36 AM
Help! Big problem, with an assignment, but not a lot of time! farmerjoe Programming 2 02-27-2005 12:12 AM
Network interfaces assignment problem Perdido Linux - Networking 5 04-24-2004 03:25 PM
IP assignment problem/router problem techrolla Linux - Networking 7 12-01-2003 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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