help with script (solved)

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
hairybiker
Level 4
Level 4
Posts: 204
Joined: Mon Nov 03, 2008 12:16 pm

help with script (solved)

Post by hairybiker »

OK having a weird problem with script
What I want it to do is to process all sub folders and files in the given folder, what it does is do the 1st sub folder only
I've added extra echo's to check the loop status
---------------cut---------------

Code: Select all

in=$1
cd "$in"
out="./burn"
pwd=$PWD
echo "PWD $pwd"
for D in ./*; do
#test for folders
  if test -d "$D"; then
      echo "Loop = $loop D=$D" "folder yes"
      cd "$D"
      echo "Processing $D"
       p1=${D##*/}
.     sub=1
       out1="$out/${pwd##*/}/$p1" 
   else
        out1="$out/${pwd##*/}"
        echo "$D not a folder"
   fi
done
-----------cut-----------------

now sending it to a folder with 2 sub folders it is only seeing the first and claims the 2nd isn't a folder.

hairybiker@octo:~/temp$ ./test.sh test
PWD /home/hairybiker/temp/test
Loop = 0 D=./1 folder yes
Processing ./1
D=./1 p1=1 pwd=/home/hairybiker/temp/test ./burn//test/1 Loop=1 sub=1
in=test pwd=/home/hairybiker/temp/test D=./1 sub=1
./2 not a folder
D=./2 p1=1 pwd=/home/hairybiker/temp/test ./burn//test Loop=2 sub=1
in=test pwd=/home/hairybiker/temp/test D=./2 sub

test contains 2 sub folders 1 & 2

Also used [ -d "$D" ] and [[ -d "$D" ]] with same result.

edited for clarity.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 4 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: help with script

Post by rene »

That's a rather strange "script".

1. Please indent; unreadable.
2. What's "$in"?
3. You assign to "$out"; should be just "out"
4. "//" is a C-style comment marker;' shell uses "#"

All that said, the specific issue you indicate is: note that you cd "$D" inside the loop, i.e., cd ./1; on the next iteration indeed no directory "./1/.2" exists: you want to again cd .. before the else.
hairybiker
Level 4
Level 4
Posts: 204
Joined: Mon Nov 03, 2008 12:16 pm

Re: help with script

Post by hairybiker »

that is a partial part of it the part l am having issue with. $in is assigned as $1 , $out is the output folder

that( // ) was added to try and make clear the problem, it isn't part of the whole script my bad.

it was indented but when pasted it got unindented. will correct
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: help with script

Post by rene »

Pasting between [code]...[/code] retains text-formatting.

As to "$out", I'm sure, but you still can/should not assign to "the value of out", i.e., $out, but to the variable out, i.e. out itself. And I trust the main question was answered?
hairybiker
Level 4
Level 4
Posts: 204
Joined: Mon Nov 03, 2008 12:16 pm

Re: help with script

Post by hairybiker »

nope the question was not answered.
there is further down the code a test for the sub=1 and if so does a cd ...
as you can see from the output it is testing ./2 and failing
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: help with script

Post by rene »

I do not understand what you're saying. In the code that you pasted after the first iteration of the for loop the working directory is the subdirectory "./1". It then in the second iteration of the loop checks for the subdirectory "./2" of that directory, i.e., the directory "./1/2" which indeed does not exist; you need to before that next iteration restore the working directory.
hairybiker
Level 4
Level 4
Posts: 204
Joined: Mon Nov 03, 2008 12:16 pm

Re: help with script

Post by hairybiker »

OK I thought it would be easier with a fragment, but no.
Here is the full script

Code: Select all

#!/bin/bash
if [ ! -d "$1" ]; then
    echo No such folder $1
	exit
fi

# test to see if we are already running
file="/home/hairybiker/.pid"
while [ -f $file ] 
do 
 echo "sleeping"
 sleep 10m
done
echo here
in="$1";
out="$2";
echo $1 $2
backup="$3";
if [ -z $backup ]
 then
   backup="/home/hairybiker/Avi/backup/"
fi
if [ -d "$backup" ];then
	mkdir -p $backup
fi
    
echo "1"> ~/.pid
echo "starting"
sub=0;
loop=0

echo Reading "$in"
cd "$in"
pwd=$PWD
echo "PWD $pwd"
for D in ./*; do

  if test -d "$D"; then
    echo "Loop = $loop D=$D" "folder yes"
    cd "$D"
    echo "Processing $D"
    p1=${D##*/}
    sub=1
    out1="$out/${pwd##*/}/$p1"  
   else
        out1="$out/${pwd##*/}"
        echo "$D not a folder"
   fi
   ((loop++));
    echo D=$D p1=$p1 pwd=$pwd $out1 Loop=$loop sub=$sub
    
     for i in { *.mkv }
    do
        format=$(mediainfo "$i")
          if [ "$format" != "" ]
              then
               if [ ! -d "$out1" ]
               then
                      mkdir -p "$out1";
               fi
           a=$(egrep -o "x265" <<<$format)
           echo "format $a" >> ~/watch.log
           b=$(egrep -o -m 1 "HEVC" <<<$format)             
           if [ "$a" != "x265" ]  &&  [ "$b" != "HEVC" ] #not an x265 already
           then
 echo             HandBrakeCLI -i "$i" -o "$out1/$i" -e x265
            else 
                cp "$i  $out1/"
            fi            
        fi
        done
    if [ sub == 1 ] 
    then
        cd "$pwd"
        sub=0
    fi
    echo in=$in pwd=$pwd D=$D sub=$sub
    from=${in##*/}/${D##*/};
    echo "Moving $from to $backup" >>~/watch.log
    echo   cp -r "$from   $backup$from"
    if [ $sub == 0 ]
    then
        break;
    fi 
#    rm -r "$from"
    
done
#clean up
rm ~/.pid
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: help with script

Post by rene »

Here you do mean if [ $sub == 1 ] rather than if [ sub == 1 ]. Better, you mean if [ $sub -eq 1 ] but in this case the string comparison also works.

BUT.

You also in that same if set sub=0 and then only a bit later break out of the loop if [ $sub == 0 ]. So that time with the correct $, still the wrong-ish == rather than -eq, but in any case the seemingly wrong program logic since you break out of the loop always now. No idea what you meant to do there.
hairybiker
Level 4
Level 4
Posts: 204
Joined: Mon Nov 03, 2008 12:16 pm

Re: help with script

Post by hairybiker »

OK it was -1 l just changed it will put it back
yes it should be $sub it is meant so that if it was in a sub folder it would move back
will make those changes and retest thanks
hairybiker
Level 4
Level 4
Posts: 204
Joined: Mon Nov 03, 2008 12:16 pm

Re: help with script

Post by hairybiker »

Nope same issue

Code: Select all

#!/bin/bash
if [ ! -d "$1" ]; then
    echo No such folder $1
	exit
fi

# test to see if we are already running
file="/home/hairybiker/.pid"
while [ -f $file ] 
do 
 echo "sleeping"
 sleep 10m
done
echo here
in="$1";
out="$2";
echo $1 $2
backup="$3";
if [ -z $backup ]
 then
   backup="/home/hairybiker/Avi/backup/"
fi
if [ -d "$backup" ];then
	mkdir -p $backup
fi
    
echo "1"> ~/.pid
echo "starting"
sub=0;
loop=0
#read -n1 -s
#
echo Reading "$in"
cd "$in"
pwd=$PWD
echo "PWD $pwd"
for D in ./*; do

  if test -d "$D"; then
    echo "Loop = $loop D=$D" "folder yes"
    cd "$D"
    echo "Processing $D"
    p1=${D##*/}
    sub=1
    out1="$out/${pwd##*/}/$p1"  
   else
        out1="$out/${pwd##*/}"
        echo "$D not a folder"
   fi
   ((loop++));
    echo D=$D p1=$p1 pwd=$pwd $out1 Loop=$loop sub=$sub
    
    echo "Out file $out1/$i" >>~/watch.log
    for i in { *.mkv }
    do
        format=$(mediainfo "$i")
          if [ "$format" != "" ]
              then
               if [ ! -d "$out1" ]
               then
                      mkdir -p "$out1";
               fi
           a=$(egrep -o "x265" <<<$format)
           echo "format $a" >> ~/watch.log
           b=$(egrep -o -m 1 "HEVC" <<<$format)             
           if [ "$a" != "x265" ]  &&  [ "$b" != "HEVC" ] #not an x265 already
           then
 echo             HandBrakeCLI -i "$i" -o "$out1/$i" -e x265
            else 
                echo "copying already x265 $1 to $out1" >>~/watch.log
                cp "$i  $out1/"
            fi            
        fi
        done
    if [ $sub eq 1 ] 
    then
        cd "$pwd"
        sub=-1
    fi
    echo in=$in pwd=$pwd D=$D sub=$sub
    from=${in##*/}/${D##*/};
    echo "Moving $from to $backup" >>~/watch.log
    echo   cp -r "$from   $backup$from"
    if [ $sub eq 0 ]
    then
        break;
    fi 
#    rm -r "$from"
    
done
#clean up
rm ~/.pid

Code: Select all

ls A
1  2

./test.sh A
here
A /exports/Avi/burn/
starting
Reading A
PWD /home/hairybiker/Avi/backup/A
Loop = 0 D=./1 folder yes
Processing ./1
D=./1 p1=1 pwd=/home/hairybiker/Avi/backup/A /exports/Avi/burn//A/1 Loop=1 sub=1
in=A pwd=/home/hairybiker/Avi/backup/A D=./1 sub=1
cp -r A/1   /exports/Avi/backup/A/1
./2 not a folder
D=./2 p1=1 pwd=/home/hairybiker/Avi/backup/A /exports/Avi/burn//A Loop=2 sub=1
in=A pwd=/home/hairybiker/Avi/backup/A D=./2 sub=1
cp -r A/2   /exports/Avi/backup/A/2
So it goes through the loop, reads 1, gets next as 2 and says it isn't a folder
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: help with script

Post by rene »

Sorry, but I'm off; what you posted now doesn't even run: eq rather than -eq is a syntax error so there's no way that that output matches that script. Giving up...
mmphosis
Level 1
Level 1
Posts: 25
Joined: Sat Apr 11, 2020 11:22 pm

Re: help with script

Post by mmphosis »

Using the wildcard * to traverse a tree of subdirectories can get pretty hairy. You want to "recurse" the tree. There must be a better way. The find command can be your friend.

Code: Select all

man find
Create a separate bash script that processes 1 item, and only 1 item (file / directory). Do all of that path mangling, media info checking, transcoding, copying, logging, and whatever else you what to do in this script. Maybe, name it: transcode_or_copy_mediafile.sh

Use the find command to traverse the directory tree, and execute your command. I would temporarily prefix an echo command just to make sure I've got the correct arguments to the transcode_or_copy_mediafile.sh command. For example:

Code: Select all

find $HOME -type f -name '*.mkv' -exec echo transcode_or_copy_mediafile.sh {} outputdir \;
The find command has lots of options. The -type f makes sure it's only working on files -- although it is still traversing all of the directories to find the files. The -name "*.mkv" option might be useful if you only want specific filename patterns.
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: help with script (solved)

Post by 1000 »

Maybe you want also read why "cd into directory in while loop doesn't work" in your script.
https://stackoverflow.com/questions/104 ... k#10446617
hairybiker
Level 4
Level 4
Posts: 204
Joined: Mon Nov 03, 2008 12:16 pm

Re: help with script (solved)

Post by hairybiker »

1000 wrote: Thu Oct 14, 2021 5:56 am Maybe you want also read why "cd into directory in while loop doesn't work" in your script.
https://stackoverflow.com/questions/104 ... k#10446617
The issue wasn't with cd'ing but in the fact the folder wasn't recognised.
I found the error and corrected it, I am more used to C programming and miss typed a variable.
Locked

Return to “Scripts & Bash”