You are not logged in.
Pages: 1
Hi, I have written this script:
#!/bin/bash
find . -name "*.j2c" | sort > frame_list
reel_maker()
{
mkdir reel_$3
while read i
do
mv -v $i reel_$3
done < <(sed -n $1,$2p frame_list)
}
for i in $*
do
if [ ! -z $3 ]
then
reel_maker $1 $2 $3
shift 3
else
:
fi
done
rm frame_list
What I need to do is to modify $1 and $2 before passing them to the reel_maker function. Example, if $1 is 199 I would like to add +1 to it and thus make it 200. I cannot figure out how to do it.
Any help is appreciated!
Last edited by xr4y (2011-10-28 21:35:50)
Offline
It depends how complex arithmetics do you require
[karol@black ~]$ cat foo
#!/bin/bash
a=$(($1+1))
echo $a
[karol@black ~]$ ./foo 1 2 3
2
but
[karol@black ~]$ ./foo a 2 3
1
so if you need to sanitize input, you have to figure something more complex.
Offline
Obviously karol, obviously. Sometimes one manages to solve the more challenging issues just to get stuck on the trivial ones. Thanks!
Offline
The main loop is so wrong...
for i in $*
You really want
while (( $# ))
...
else
break
...
-- EDIT --
And in direct answer to your question in the title,
$ set -- foo bar
$ echo "$2"
bar
Last edited by lolilolicon (2011-10-07 08:19:42)
This silver ladybug at line 28...
Offline
Thanks lolilolicon, I corrected the main loop but I do not understand how "set" would work here as oppsed to what karol posted:
#!/bin/bash
find . -name "*.j2c" | sort > frame_list
reel_maker()
{
mkdir reel_$3
while read i
do
mv -v $i reel_$3
done < <(sed -n $1,$2p frame_list)
}
while (( $# ))
do
if [ ! -z $3 ]
then
a=$(($1+1))
b=$(($2+1))
reel_maker $a $b $3
shift 3
else
break
fi
done
rm frame_list
Offline
I think there is no more need for the "if":
while (( $# ))
do
a=$(($1+1))
b=$(($2+1))
reel_maker $a $b $3
shift 3
done
Offline
Thanks lolilolicon, I corrected the main loop but I do not understand how "set" would work here as oppsed to what karol posted:
That was in direct answer to the title "Modify $1 $2", not saying that you should use it in such a case, e.g.,
#!/bin/bash
i=$(modify "$1")
j=$(modify "$2")
shift 2
set -- "$i" "$j" "$@"
Also, you should pay attention to properly quoting the variables.
Another thing to note, is that directly evaluating user input as arithmetic expression is bad. For one thing, large values like 2**32**64 will make your machine scream; for another, you can run into fun stuff like this one:
a=a
b=$(($1+1))
Pass 'a' as $1 and you get a good recursion -- but because it will work if you pass other values to it, it will seem to work, which hides the potential risk. This is the worst kind of bug.
This silver ladybug at line 28...
Offline
I think there is no more need for the "if":
while (( $# )) do a=$(($1+1)) b=$(($2+1)) reel_maker $a $b $3 shift 3 done
That would get into a dead loop as long as the number of positional arguments is not divisible by 3, because the 'shift 3' will not shift at all when there are less than 3 positional arguments left, but it will return greater than 0, so you can use
shift 3 || break
but then, when there're less than 3 arguments left, reel_maker will break. So your original if-else is really better.
Last edited by lolilolicon (2011-10-07 10:15:42)
This silver ladybug at line 28...
Offline
lolilolicon, thanks for taking time and explaining.
Offline
I rewrote the script since this minimizes the arguments fed to this script from our NLE chapter marks. This method also feels faster than the previous but since I'm pretty much still a noob at scripting/programming (film editor by profession), can someone enlighten me if there is any better way to write/modify this script to make it move the chunks of files to their destined directories even faster?
This script is for moving 100k+ of jpeg2000 frames into their respective folders for DCP delivery!
Example:
reel_maker.sh 16667 33334 50000 66667 83333 99999
#!/bin/bash
find . -name "*.j2c" | sort > frame_list
reel_maker() {
mkdir reel_$3
sed -n "$1","$2"p frame_list | xargs -i mv '{}' reel_$3
}
reel=1
while (( $# )); do
if [ ! -d reel_1 ]; then
reel_maker "1" "$1" "$reel"
let reel++
elif [ ! -z $3 ]; then
a=$(($1+1))
reel_maker "$a" "$2" "$reel"
let reel++
shift 1
else
a=$(($1+1))
b=$(($2+1))
reel_maker "$a" "$b" "$reel"
break
fi
done
rm frame_list
Last edited by xr4y (2011-10-28 21:43:04)
Offline
Pages: 1