LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2011, 04:11 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
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
??

Last edited by metallica1973; 10-11-2011 at 01:46 PM.
 
Old 10-10-2011, 07:31 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
'find $(cat include_dirs) -type f | cpio --whatever-args'?
 
Old 10-11-2011, 01:40 PM   #3
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Thanks

Is the whole line enclosed in single quotes ''

Code:
	'find $(cat include_dirs) -type f | cpio --whatever-args'
??
 
Old 10-11-2011, 02:12 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by metallica1973 View Post
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.
 
Old 10-11-2011, 02:48 PM   #5
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
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

Last edited by metallica1973; 10-11-2011 at 02:53 PM.
 
Old 10-11-2011, 03:03 PM   #6
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Cool

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

Last edited by metallica1973; 10-11-2011 at 03:04 PM.
 
Old 10-12-2011, 11:57 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by metallica1973 View Post
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).
 
Old 10-13-2011, 10:14 AM   #8
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
Many thanks

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

Regards

Last edited by metallica1973; 10-13-2011 at 10:16 AM.
 
  


Reply



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
Need File Name comparison help on Korn Shell Script confusiac437 Programming 7 07-01-2011 02:14 PM
Korn shell script mmahulo Linux - Newbie 9 12-17-2008 09:19 AM
Korn shell leave only one carriage return at the end of file quadmore Programming 6 07-14-2008 09:46 AM
korn shell: scripting an or for if..then Harry Seldon Linux - General 2 01-10-2008 04:21 PM
Korn Shell Coprocesses and File Descriptors tmarikle Programming 7 01-25-2005 09:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 PM.

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
Open Source Consulting | Domain Registration