LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CAT file then to CPIO using Korn Shell (https://www.linuxquestions.org/questions/programming-9/cat-file-then-to-cpio-using-korn-shell-907462/)

metallica1973 10-10-2011 04:11 PM

CAT file then to CPIO using Korn Shell
 
I am messing with a backup script that I have and ran into issue. In this case I want to extract the directories and files that I want to backup from a file. I have a file named: include_dirs which includes a short list of files and directories for testing.

include_dirs:

Code:


/home/testuser/
/home/testuser/file1
/home/testuser/file2
/home/testuser/file3

Code:

dirs=include_dirs # the files and directories I want to back up
date=$(date +%m%d%y)
cat $dirs | while read lines
        do
                echo $lines| cpio -oavc > /home/testuser/extract/full$date.cpio 2> /home/testuser/extract/full_status$date.log

        done

When I run it I get: ----> I truncated the other gargage

Code:

ksh -x ./test_backup full
+ dirs=include_dirs
+ [[ -d /home/testuser/extract ]]
+ [[ full == full ]]
+ read directories
+ cat include
+ cpio -oavc
+ echo /home/testuser/file1
+ 1> /home/testuser/extract/full101011.cpio 2> /home/testuser/extract/full_status101011.log
+ read directories
+ cpio -oavc
+ echo /home/testuser/file2
+ 1> /home/testuser/extract/full101011.cpio 2> /home/testuser/full_status101011.log

It keeps recreating the cpio file and then eventually ends. I think it has something to due with echoing the file and then piping it to cpio. Would this suffice:

Code:

dirs=include_dirs # the files and directories I want to back up
date=$(date +%m%d%y)
cat $dirs | while read lines
        do
              cat $lines| cpio -oavc < $lines > /home/testuser/extract/full$date.cpio 2> /home/testuser/extract/full_status$date.log

        done

??

unSpawn 10-10-2011 07:31 PM

'find $(cat include_dirs) -type f | cpio --whatever-args'?

metallica1973 10-11-2011 01:40 PM

Thanks

Is the whole line enclosed in single quotes ''

Code:

        'find $(cat include_dirs) -type f | cpio --whatever-args'
??

unSpawn 10-11-2011 02:12 PM

Quote:

Originally Posted by metallica1973 (Post 4495712)
Is the whole line enclosed in single quotes ''

No just
Code:

find $(cat include_dirs) -type f | cpio --whatever-args
and make sure you replace "--whatever-args" with something useful.

metallica1973 10-11-2011 02:48 PM

thanks,

the interesting part is that everything works find from the cli and not in the script so if I do this the commands work:

Code:


dirs=include_test
cat $dirs | cpio -oavc > /media/caca/extract/test_data.cpio

But if I just create a while.ksh script and have this:

Code:

#!/bin/ksh
date=$(date +%m%d%y)
dirs=include_test

cat $dirs | while read directories
         
        do
                cat $directories | cpio -oavc > /home/testuser/extract/full$date.cpio
done

it just hangs. I tried your method and it does work.

Code:

#!/bin/ksh
date=$(date +%m%d%y)
dirs=include_test

cat $dirs | while read directories
         
        do
       
find $(cat $dirs) -type f | cpio -oavc > /home/testuser/extract/full$date.cpio 2> /home/testuser/extract/full$date.log

done

When I execute what you have the script keeps looping and never ends. I can just tail the full$date.log and see the loop. It just starts all over again. It may be my loop structure

regards

metallica1973 10-11-2011 03:03 PM

It looks like I had an id10t error. I needed the exit.

Code:

#!/bin/ksh
date=$(date +%m%d%y)
dirs=include_test

cat $dirs | while read directories
         
        do

find $(cat $dirs) -type f | cpio -oavc > /home/testuser/extract/full$date.cpio 2> /home/testuser/extract/full$date.log

exit
done


unSpawn 10-12-2011 11:57 AM

Quote:

Originally Posted by metallica1973 (Post 4495779)
It looks like I had an id10t error. I needed the exit.

No, that's not it. Written out in full:
Code:

#!/bin/ksh
# - Adding paths ensures that things work even if items isn't in the PATH.
# - Using all-caps variable names makes them easier to spot.
# - I prefer YYYY/MM/DD-style dates as they're easier to sort:
CFGDATE=$(/bin/date +'%Y%m%d')
CFGFILE="/path/to/include_test"
CFGEXTRACTPATH="/home/testuser/extract"

# Loop over the contents of /path/to/include_test, and for each item do:
cat "${CFGFILE}" | while read DIRNAME; do
 # Check if the variable is empty:
 if [ -z "${DIRNAME}" ]; then exit 1; fi
 # Check if the directory exists:
 if [ ! -d "${DIRNAME}" ]; then exit 1; fi
 # Find files in item DIRNAME discarding errors
 # and exit the copio process on error
 find "${DIRNAME}" -type f 2>/dev/null | \
 cpio -oavc > "${CFGEXTRACTPATH}/full${CFGDATE}.cpio" \
 2> "${CFGEXTRACTPATH}/full${CFGDATE}.log" || \
 { echo "Unable to cpio-process \"${CFGEXTRACTPATH}/full${CFGDATE}.cpio\", exiting."; exit 1; }
# After all items are processed exit the loop
done
# Exit the script
exit 0

* I haven't checked this in Korn BTW as I'm more accustomed to Heirloom Bourne and Bourne Again shells so YMMMV(VM).

metallica1973 10-13-2011 10:14 AM

Many thanks

I will create a new post with my next questions on detecting whitespaces

Regards


All times are GMT -5. The time now is 05:58 AM.