LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-21-2017, 01:42 PM   #1
mackowiakp
Member
 
Registered: Jun 2014
Location: Poland/Gdynia
Distribution: Mageia 7, SH4, Debian
Posts: 316

Rep: Reputation: 8
Passing variable name to for-do-done loop


I use surveillance IP cameras. This cameras FTP`s video clip and photos to server. Each camera transmit own materials to catalogue named Typeofcamera_MACaddress for example FI9900P_00626E660E5A.
I convert video clips from MKV to MP4 and than I want to move such concerted file to subdir named Camera1, Camera2 etc.
So my code should looks like this

Code:
Camera1=/path_to_camera_1
Camera2=/path_to_camera_2
Camera3=/path_to_camera_3

for i in $Camera1 $Camera2 $Camera3
do
cd $i
for plik in `find . -maxdepth 1 -type f -name '*.mp4'`
do
#
# The line below should contain Name_of_variable_i not content
# of variable. That is Camera1, Camera2 etc..
# So what should be in place of Name_of_variable_i ?
#
mv $plik /New_path/Name_of_variable_i/
done
done
So my question is. What should be in place of Name_of_variable_i ?
Or in other way. How to obtain string with the name of variable in loop. I mean the name not content of variable.
Maybe my question is strange but the sample below is dynamic in reality and much more complicated then I presented. So because of simplicity I removed non necessary at this moment elements. .
 
Old 05-21-2017, 03:00 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
From the example here: http://tldp.org/LDP/abs/html/bashver2.html#EX78
Code:
#!/bin/bash
Camera1=/path_to_camera_1
Camera2=/path_to_camera_2
Camera3=/path_to_camera_3

for i in Camera1 Camera2 Camera3
do
echo $i ${!i}  
done
Output:
Quote:
Camera1 /path_to_camera_1
Camera2 /path_to_camera_2
Camera3 /path_to_camera_3
 
1 members found this post helpful.
Old 05-21-2017, 03:57 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
it is confusing to me.
You are searching in 3 different dir converting them into mp4? then moving them into 3 new different directories?

taking your for loops and using two while loops instead. Because I do not the results of them for loops and like that cd $i thing to get to your files. - bugs me ...

where is your converting mkv to mp4 code?
how and when is that being done?

or is this script of yours after the fact of converting mkv to mp4? where hopefully it could be all done within one script.

anyways this script shows it searching 3 separate dir, then moving every "found file type searched for" out of that dir and into 3 new different dir in sequence. Or you can change it to go into one dir only. simple mod.

Trying to convey two separate thoughts at once here.
Code:
#!/bin/bash

a=0
Camera=( /media/data/Music-2/10cc /media/data/Music-2/Aliotta-Haynes-Jeremiah /media/data/Music-2/Blue-Swede )

NewDir=( /new/dir1 /new/dir2 /new/dir3 )

while [[ $a -ne 3 ]]
do
{

     while read FILENAME
     do
     {

# to convert mkv to mp4 could be done in here, depending on what you're using to 
#do that with. 
#then in here their would be a bit of more code for your converting into mp4 then
# taking that new file and moving into to these dir etc.. 
#
# Code to get path and filename being searched
#then cutting the string up to get the fileName only no ext.
#
# using that to recreate a NEWname="newName.mp4" or NewName="$OldName.mp4"
#
# code to lead to the path of new file then moving that into the other directory.
#
#  (after thought) that is what you are looking for isn't it?
# find mkv convert to mp4 then move that mp4 without having to look for it?
# I do not know yours says mp4 already so I am using mp3 for an actual data output
# to show what this does. 
# 
#
echo "mkdir -p ${NewDir[$a]}"

echo "$a - mv $FILENAME ${NewDir[$a]}"

      }
      done< <(find "${Camera[$a]}" -type f -name "*.mp3")

((a++))

}
done
results of that code. searches three different directories then creates new dirs then moves files from each separate directory into new directories 3 total in sequence.

Code:
mkdir -p /new/dir1
0 - mv /media/data/Music-2/10cc/Guardians-Of-The-Galaxy/106-10cc-im_not_in_love.mp3 /new/dir1
mkdir -p /new/dir2
1 - mv /media/data/Music-2/Aliotta-Haynes-Jeremiah/Guardians-Of-The-Galaxy-Awesome-Mix-Vol.-2/03-aliotta_haynes_jeremiah-lake_shore_drive.mp3 /new/dir2
mkdir -p /new/dir3
2 - mv /media/data/Music-2/Blue-Swede/Guardians-Of-The-Galaxy/101-blue_swede-hooked_on_a_feeling.mp3 /new/dir3
to take that new file you have to know where it is being created at. then use that path in place of the FILENAME var then move it into the 3 different dir. If you were creating mp4 out of mkv just do something like this.

Code:
inner loop
f=$FILENAME
chop up $f to get name of file
NewFile="$file.mp4"
convert file
"$FILENAME" covert code - (output) "$NewFile"
mv -v "$pathOfNewFile/$NewFile"   "${NewDir[$a]}"
done 
#move to next file - Loop repeats as necessary
if you are using something other than handbreak cli to convert as it stops after each run of one file then has to be reset to run another one. it is like NO Way to run it in a script on multiple files one after the other. I've tried it and researched it. unless they fixed it. It has been about a year since I've tried that.

anyways if what you are using to convert your mkv to mp4 can be ran on the command line it could be put into a script like the one above.

search for your mkv then convert it into an .mp4 then take that file where ever it is at then move it into the 3 different directories accordingly, or delete the mkv and move the new one back in its place or make a copy and move one back and the other one somewhere else etc...

your questions
Quote:
So my question is. What should be in place of Name_of_variable_i ?
when moving "from" "to" no name is required for the "to" if one is present in the "from". which has to be.

Code:
mv /path/filename /path/to
that is all that is needed unless a name change is taking place during the move.
Quote:
Or in other way. How to obtain string with the name of variable in loop. I mean the name not content of variable.
you want to get the name of the variable your using as a string that needs it to be placed into another variable to get that contents of it in a string
Code:
varName="contents"

StingOfVarNameWithOutItsContents="varName"

echo "$StingOfVarNameWithOutItsContents"

varName
or
Code:
for i in Camera1 Camera2 Camera3
do
echo $i ${!i}  
done
which gives you the var name and its contents.

I said it was confusing to me.
I just hope I didn't confuse you.

Last edited by BW-userx; 05-21-2017 at 06:48 PM.
 
1 members found this post helpful.
Old 05-22-2017, 01:10 PM   #4
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
you might try -iname? So you get the .MP4 and .mp4 files, plus .Mp4 and .mP4, since names are more informative than functional in linux as well as case sensitive by default.

As for the other part, change the first for vars.

Code:
for i in Camera1 Camera2 Camera3
do
cd $(eval echo \$$i)
And then you could just use $i on that other part.

Code:
mv $plik /New_path/$i/
 
1 members found this post helpful.
Old 05-22-2017, 01:33 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
your FOR LOOP
Code:
#!/bin/bash

#Camera=( /media/data/Music-2/10cc /media/data/Music-2/Aliotta-Haynes-Jeremiah /media/data/Music-2/Blue-Swede )

dir1=/media/data/Music-2/10cc
dir2=/media/data/Music-2/Aliotta-Haynes-Jeremiah
dir3=/media/data/Music-2/Blue-Swede

a=1

copy_to=/media/data/CRAP-TEST

for i in $dir1 $dir2 $dir3
do
cd $i
for plik in `find . -type f -name '*.mp3'`
do

FileName=${plik##*/} <-- gets file name off of plik
or make a differnet name

NewFile="whateverIfeelLikecallingit-($a).mp4"
((a++))

echo
echo "FileName: $FileName"
echo "plik: $plik"
echo "i: $i"
echo
mkdir -p "$copy_to"

cp -v "$plik" "$copy_to/$FileName"
or this
mv -v "$plik" "$copy_to/$NewFile"
or just this
mv -v "$plik" "$copy_to"

done
done
because $plik has all of the info needed, unless you want to change the file name.
results
Code:
userx%voider ⚡ testing ⚡> ./4forLoop

FileName: 106-10cc-im_not_in_love.mp3
plik: ./Guardians-Of-The-Galaxy/106-10cc-im_not_in_love.mp3
i: /media/data/Music-2/10cc

'./Guardians-Of-The-Galaxy/106-10cc-im_not_in_love.mp3' -> '/media/data/CRAP-TEST/106-10cc-im_not_in_love.mp3'

FileName: 03-aliotta_haynes_jeremiah-lake_shore_drive.mp3
plik: ./Guardians-Of-The-Galaxy-Awesome-Mix-Vol.-2/03-aliotta_haynes_jeremiah-lake_shore_drive.mp3
i: /media/data/Music-2/Aliotta-Haynes-Jeremiah

'./Guardians-Of-The-Galaxy-Awesome-Mix-Vol.-2/03-aliotta_haynes_jeremiah-lake_shore_drive.mp3' -> '/media/data/CRAP-TEST/03-aliotta_haynes_jeremiah-lake_shore_drive.mp3'

FileName: 101-blue_swede-hooked_on_a_feeling.mp3
plik: ./Guardians-Of-The-Galaxy/101-blue_swede-hooked_on_a_feeling.mp3
i: /media/data/Music-2/Blue-Swede

'./Guardians-Of-The-Galaxy/101-blue_swede-hooked_on_a_feeling.mp3' -> '/media/data/CRAP-TEST/101-blue_swede-hooked_on_a_feeling.mp3'
in new dir
Code:
userx%voider ⚡ testing ⚡> ls /media/data/CRAP-TEST
03-aliotta_haynes_jeremiah-lake_shore_drive.mp3  101-blue_swede-hooked_on_a_feeling.mp3  106-10cc-im_not_in_love.mp3
userx%voider ⚡ testing ⚡>
I removed -maxdepth 1 to this script because the files I am using are within its base dir. so that was not working in this example.

Last edited by BW-userx; 05-22-2017 at 01:54 PM.
 
1 members found this post helpful.
  


Reply

Tags
bash scripting, variables


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
passing input arguments from a file in a loop mstfkpln Linux - Newbie 7 03-11-2016 12:22 AM
For Loop passing the index to filename g_paschoal Programming 8 11-01-2013 11:14 AM
SH: Passing a variable out of a "while ; do ; done" loop frenchn00b Programming 13 11-10-2011 12:48 AM
[SOLVED] Passing a list of variables to a for loop pdr_dan Linux - Newbie 5 03-21-2011 12:38 PM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM

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

All times are GMT -5. The time now is 01:14 AM.

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