LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-16-2018, 09:01 AM   #1
Viramith
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Rep: Reputation: Disabled
Bash scripts


hello everyone
first time in this forum so i hope i will explain my problem as coherent as possible
i was asked to create a loop in script which will create 5 filtered files for each file with the following freq values:
0.1
0.2
.
.
0.5
afterwards they have requested to write in the loop a command which its output needs to generate new directory for each filtered file and every new dir needs to contain the filtered file , like so:
0.1 dir contains 0.1 filtered file
0.2 dir contains 0.2 filtered file
and so on..
the script that i have used:
#!/bin/bash

file=$1

for az in `seq 0.1 0.1 0.5`

do
mkdir $az
e2proc3d.py --process=filter.lowpass.gauss.cutoff_freq=0.1 $file ${file%.mrc}.filt$az.mrc
done

can you please guide me for what's missing?
thanks for the help
 
Old 11-16-2018, 09:37 AM   #2
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 288Reputation: 288Reputation: 288
Please use [CODE] tags (see my sig).
Why are you using e2proc3d.py script? Is it directly related to your question? If yes, please provide us with more details about your whole requirement.
Please paste your script output so we can know how to debug it.
 
Old 11-16-2018, 09:53 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
this is so homework, well first things first, code tags to format your code, then steps needed to created directory by a name given using a variable then move the corresponding file(s) into same said freshly created directory.
by the use of the terms used, freq values, and filtered file. I am assuming that is just names of files using them values.

create directory with same name as file. then move same file into that directory. using the for loop in the manner you are you have no path to file, if they are in the current directory then the use of Print Working Directory (pwd)should come in handy.

for example.
Code:
for i in $(ls) ; do echo $(pwd)/$i ; done
then just incorporate that into your code to use it to move your files into the directories you've created.

Issue you cannot have two same dir and file in same parent dir as both are seen as a file in Linux. therefore you have to create the dir somewhere else then move the files into them. so the basics as I do not see exactly what your file names are. I can only guess what the look like by the code you're using to cut them up.

but this is an issue for you to figure out, this is what I'd do having unknowns.
Code:
#files in dir already
$ ls
0  0.1  0.2  0.3  0.4  0.5  1  2

#creating a variable to hold a path to

makedir="$(pwd)/sub"

$ echo $makedir
/home/userx/test/sub

#running a one liner off he cli

$ for i in `seq 0.1 0.1 0.5` ; do mkdir -p  "$makedir"/"$i" ; mv -v $(pwd)/$i $makedir/$i ; done
renamed '/home/userx/test/0.1' -> '/home/userx/test/sub/0.1/0.1'
renamed '/home/userx/test/0.2' -> '/home/userx/test/sub/0.2/0.2'
renamed '/home/userx/test/0.3' -> '/home/userx/test/sub/0.3/0.3'
renamed '/home/userx/test/0.4' -> '/home/userx/test/sub/0.4/0.4'
renamed '/home/userx/test/0.5' -> '/home/userx/test/sub/0.5/0.5'

#checking results

userx@SlackOLatern.net:~/test
$ ls
0  1  2  sub
userx@SlackOLatern.net:~/test
$ ls */*
sub/0.1:
0.1

sub/0.2:
0.2

sub/0.3:
0.3

sub/0.4:
0.4

sub/0.5:
0.5
though yours might be little more complected due to I see you're cutting the name up, the basics are still the same, so you'll just need to experiment.

Last edited by BW-userx; 11-16-2018 at 10:00 AM.
 
Old 11-16-2018, 09:56 AM   #4
Viramith
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
script

Quote:
Originally Posted by l0f4r0 View Post
Please use [CODE] tags (see my sig).
Why are you using e2proc3d.py script? Is it directly related to your question? If yes, please provide us with more details about your whole requirement.
Please paste your script output so we can know how to debug it.
yes it is directly related to my question.
the output of the script should create 5 new filtered files in the current directory
in one picture there is the full script.
in the other picture is the output of the executed script.
you should see that there are "5IRZ.filt0.1.mrc" "5IRZ.filt0.2.mrc" and so on..
0.1 0.2 0.3 0.4 0.5 directories
i want to put each .mrc into its appropriate dir
5IRZ.filt0.1.mrc -> 0.1 dir
and so on
thanks
Attached Thumbnails
Click image for larger version

Name:	Spectacle.J16052.png
Views:	14
Size:	185.9 KB
ID:	28992   Click image for larger version

Name:	Spectacle.Z15773.png
Views:	12
Size:	51.3 KB
ID:	28993  
 
Old 11-16-2018, 10:03 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
I hate photos sometimes, they are way too small. now you also need to learn how to copy, paste into code block in LQ, so everyone can read what you're reading.

look back at me post #3. you look by that code you posted to now how to cut in down to just getting the numbers off the file name, I gave you the rest already, basically.
 
Old 11-16-2018, 10:03 AM   #6
Viramith
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
this is so homework, well first things first, code tags to format your code, then steps needed to created directory by a name given using a variable then move the corresponding file(s) into same said freshly created directory.
by the use of the terms used, freq values, and filtered file. I am assuming that is just names of files using them values.

create directory with same name as file. then move same file into that directory. using the for loop in the manner you are you have no path to file, if they are in the current directory then the use of Print Working Directory (pwd)should come in handy.

for example.
Code:
for i in $(ls) ; do echo $(pwd)/$i ; done
then just incorporate that into your code to use it to move your files into the directories you've created.

Issue you cannot have two same dir and file in same parent dir as both are seen as a file in Linux. therefore you have to create the dir somewhere else then move the files into them. so the basics as I do not see exactly what your file names are. I can only guess what the look like by the code you're using to cut them up.

but this is an issue for you to figure out, this is what I'd do having unknowns.
Code:
#files in dir already
$ ls
0  0.1  0.2  0.3  0.4  0.5  1  2

#creating a variable to hold a path to

makedir="$(pwd)/sub"

$ echo $makedir
/home/userx/test/sub

#running a one liner off he cli

$ for i in `seq 0.1 0.1 0.5` ; do mkdir -p  "$makedir"/"$i" ; mv -v $(pwd)/$i $makedir/$i ; done
renamed '/home/userx/test/0.1' -> '/home/userx/test/sub/0.1/0.1'
renamed '/home/userx/test/0.2' -> '/home/userx/test/sub/0.2/0.2'
renamed '/home/userx/test/0.3' -> '/home/userx/test/sub/0.3/0.3'
renamed '/home/userx/test/0.4' -> '/home/userx/test/sub/0.4/0.4'
renamed '/home/userx/test/0.5' -> '/home/userx/test/sub/0.5/0.5'

#checking results

userx@SlackOLatern.net:~/test
$ ls
0  1  2  sub
userx@SlackOLatern.net:~/test
$ ls */*
sub/0.1:
0.1

sub/0.2:
0.2

sub/0.3:
0.3

sub/0.4:
0.4

sub/0.5:
0.5
though yours might be little more complected due to I see you're cutting the name up, the basics are still the same, and you'll just need to experiment.
well, i have to say its hard to understand the level of scripting you used. i'm in linux class and we didnt touch this kind of level. the last part of moving the files into the dirs should be with "mv" command (i think thats what the tutor wanted)
i have tried to add this, but its a bit messy:
mv /home/emstudent/ex33/rot/5IRZ.filt0.1.mrc /home/emstudent/ex33/rot/0.1
mv /home/emstudent/ex33/rot/5IRZ.filt0.2.mrc /home/emstudent/ex33/rot/0.2
mv /home/emstudent/ex33/rot/5IRZ.filt0.3.mrc /home/emstudent/ex33/rot/0.3
mv /home/emstudent/ex33/rot/5IRZ.filt0.4.mrc /home/emstudent/ex33/rot/0.4
mv /home/emstudent/ex33/rot/5IRZ.filt0.5.mrc /home/emstudent/ex33/rot/0.5

if there is a 1-2 line command which allow me to move each file to its destined dir, that would be the best
thanks
 
Old 11-16-2018, 10:12 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
Quote:
Originally Posted by Viramith View Post
well, i have to say its hard to understand the level of scripting you used. i'm in linux class and we didnt touch this kind of level. the last part of moving the files into the dirs should be with "mv" command (i think thats what the tutor wanted)
i have tried to add this, but its a bit messy:
mv /home/emstudent/ex33/rot/5IRZ.filt0.1.mrc /home/emstudent/ex33/rot/0.1
mv /home/emstudent/ex33/rot/5IRZ.filt0.2.mrc /home/emstudent/ex33/rot/0.2
mv /home/emstudent/ex33/rot/5IRZ.filt0.3.mrc /home/emstudent/ex33/rot/0.3
mv /home/emstudent/ex33/rot/5IRZ.filt0.4.mrc /home/emstudent/ex33/rot/0.4
mv /home/emstudent/ex33/rot/5IRZ.filt0.5.mrc /home/emstudent/ex33/rot/0.5

if there is a 1-2 line command which allow me to move each file to its destined dir, that would be the best
thanks
that is exactly what he wants, create dir by file name then move the files into the dir. like you have written there.


post your script, you already have your basic code to get what you need posted. looks like you covered variables. this is all you need to use as you are already using them along with cut command and a python script.

this here your code, is a one by one manual script, meaning you have to add the file name manually. then use $1 to get that name for processing.

Code:
#!/bin/bash

file=$1

for az in `seq 0.1 0.1 0.5`

do
mkdir $az
e2proc3d.py --process=filter.lowpass.gauss.cutoff_freq=0.1 $file ${file%.mrc}.filt$az.mrc
done
that looks to be your entire script due to the shee bang up top.

I'd used pure bash to cut the string, setup where from and where to variables to use, and one or two to hold the before name of file, and after name of file.

Knowing what you're looking for ahead of time the 0.1 - 0.5 you can cheat by setting that up to make the dir using just that information. then match the file to it then move it, or cut the part out that is 0.1 - 0.5 then use that, it is your paths needed, where are they coming from and where are you putting them that is now put into question.

how do you get them?

given your path in your post, use that.

slow down then, open a terminal then, run my code as echo so you can see what it is doing. separate them variables echo $(pwd) etc.

Last edited by BW-userx; 11-16-2018 at 10:21 AM.
 
1 members found this post helpful.
Old 11-16-2018, 10:17 AM   #8
Viramith
LQ Newbie
 
Registered: Nov 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
that is exactly what he wants, create dir by file name then move the files into the dir. like you have written there.


post your script, you already have your basic code to get what you need posted. looks like you covered variables. this is all you need to use as you are already using them along with cut command and a python script.

this here your code, is a one by one manual script, meaning you have to add the file name manually. then use $1 to get that name for processing.

Code:
#!/bin/bash

file=$1

for az in `seq 0.1 0.1 0.5`

do
mkdir $az
e2proc3d.py --process=filter.lowpass.gauss.cutoff_freq=0.1 $file ${file%.mrc}.filt$az.mrc
done
that looks to be your entire script due to the shee bang up top.

I'd used pure bash to cut the string, setup where from and where to variables to use, and one or two to hold the before name of file, and after name of file.

Knowing what you're looking for ahead of time the 0.1 - 0.5 you can cheat by setting that up to make the dir using just that information. then match the file to it then move it, or cut the part out that is 0.1 - 0.5 then use that, it is your paths needed, where are they coming from and where are you putting them that is now put into question.

how do you get them?

given your path in your post, use that.

run my code as echo so you can see what it is doing. separate them variables echo $(pwd) etc.
i think i got what i need
thanks a lot
 
Old 11-16-2018, 10:34 AM   #9
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by Viramith View Post
i think i got what i need
Please share your answer when you will be done with it and mark your thread as "SOLVED" after that (see my sig).
 
Old 11-16-2018, 10:40 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
this is pure bash, using known file names and what I need to extract off of them to get it done. I created files named like you shown in your post
Code:
$ ls
5IRZ.filt0.0.mrc  5IRZ.filt0.1.mrc  5IRZ.filt0.2.mrc  5IRZ.filt0.3.mrc  5IRZ.filt0.4.mrc  5IRZ.filt0.5.mrc
then using bash to strip the file string down to a number using echo to show what it will do without it actually doing it first.

Code:
$ for i in $(ls) ; do echo $i ; f1=${i#*0.} ; f1=${f1%%.*} ; echo $f1 ; echo "mkdir $(pwd)/"0.$f1" ; echo "mv $(pwd)/$i $(pwd)/"0.$f1" ;  done
5IRZ.filt0.0.mrc
0
mkdir /home/userx/test/0.0 ; echo mv /home/userx/test/5IRZ.filt0.0.mrc /home/userx/test/0.0
5IRZ.filt0.1.mrc
1
mkdir /home/userx/test/0.1 ; echo mv /home/userx/test/5IRZ.filt0.1.mrc /home/userx/test/0.1
5IRZ.filt0.2.mrc
2
mkdir /home/userx/test/0.2 ; echo mv /home/userx/test/5IRZ.filt0.2.mrc /home/userx/test/0.2
5IRZ.filt0.3.mrc
3
mkdir /home/userx/test/0.3 ; echo mv /home/userx/test/5IRZ.filt0.3.mrc /home/userx/test/0.3
5IRZ.filt0.4.mrc
4
mkdir /home/userx/test/0.4 ; echo mv /home/userx/test/5IRZ.filt0.4.mrc /home/userx/test/0.4
5IRZ.filt0.5.mrc
5
mkdir /home/userx/test/0.5 ; echo mv /home/userx/test/5IRZ.filt0.5.mrc /home/userx/test/0.5
you can find how to strip the string on this page under Substring Removal
https://www.tldp.org/LDP/abs/html/st...ipulation.html

now let us see what you came up with.

Last edited by BW-userx; 11-16-2018 at 10:45 AM.
 
  


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
LXer: Python Scripts as a Replacement for Bash Utility Scripts LXer Syndicated Linux News 1 01-17-2013 08:08 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 02:40 AM
Where are BASH commands stored? Not scripts but what bash exe uses. theKbStockpiler Programming 11 02-23-2011 03:06 PM
KDE 4 Autostart Program Bash Script to Write Autostart Bash Scripts for You! jdmcdaniel3 SUSE / openSUSE 1 05-03-2010 06:17 AM
[SOLVED] Finding bugs in bash scripts, Analyis tool for bash traene Programming 2 10-31-2009 11:42 AM

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

All times are GMT -5. The time now is 01:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration