You are not logged in.

#1 2011-10-06 20:14:20

xr4y
Member
Registered: 2011-05-06
Posts: 33

[SOLVED] Modify $1 $2

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

#2 2011-10-06 21:28:31

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Modify $1 $2

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

#3 2011-10-07 07:41:31

xr4y
Member
Registered: 2011-05-06
Posts: 33

Re: [SOLVED] Modify $1 $2

Obviously karol, obviously. Sometimes one manages to solve the more challenging issues just to get stuck on the trivial ones. Thanks!

Offline

#4 2011-10-07 08:02:47

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] Modify $1 $2

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

#5 2011-10-07 09:39:58

xr4y
Member
Registered: 2011-05-06
Posts: 33

Re: [SOLVED] Modify $1 $2

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

#6 2011-10-07 09:47:08

xr4y
Member
Registered: 2011-05-06
Posts: 33

Re: [SOLVED] Modify $1 $2

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

#7 2011-10-07 10:09:00

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] Modify $1 $2

xr4y wrote:

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

#8 2011-10-07 10:12:25

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: [SOLVED] Modify $1 $2

xr4y wrote:

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

#9 2011-10-07 10:43:07

xr4y
Member
Registered: 2011-05-06
Posts: 33

Re: [SOLVED] Modify $1 $2

lolilolicon, thanks for taking time and explaining.

Offline

#10 2011-10-27 10:52:36

xr4y
Member
Registered: 2011-05-06
Posts: 33

Re: [SOLVED] Modify $1 $2

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

Board footer

Powered by FluxBB