Not correct processing of “\ “ in names of dirs inside shell script (tar command - system backup scr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not correct processing of “\ “ in names of dirs inside shell script (tar command - system backup scr
# 1  
Old 06-07-2011
Not correct processing of “\ “ in names of dirs inside shell script (tar command - system backup scr

Hello,

Recently, I've started with shell scripting, and decided to write a script for my system backup using tar. When I was dealing with tar execution inside shell script I found this, inside shell we have the following code:

Code:
tar $TAR_PARAMS $ARCHIVE_FILE $EXCLUDE $BACKUP_START

with reflection

Code:
tar -cvpzf /home/ilnar/Doenloads/laptop_backup_2011_06_04_125909.tar.gz  --exclude=/home/ilnar/Downloads/laptop_backup_2011_06_04_125909.tar.gz 
--exclude=/proc 
--exclude=/dev
(etc... further home dirs)
--exclude=/home/ilnar/Desktop
--exclude=/home/ilnar/Pictures
(etc..AND MAIN QUESTION)
--exclude=/home/ilnar/Documents
--exclude=/home/dasha/my\ songs
(start from...)
/

So, command should exclude “my songs” from gzip archive, bit it doesn't. If I execute this command directly in shell, everything is ok, but when the same is in shell script it doesn't work! I've tested with:
Code:
echo tar $TAR_PARAMS $ARCHIVE_FILE $EXCLUDE $BACKUP_START

I'm using ubuntu 11.04

(Please note: with my backup and restore scripts I want to exclude all users folders with files like heavy video and audio etc., but not users settings (like .hidden_files))

I'm writing about “spaces” because”, my wife Dasha may create such directories. If you want to look on my scripts, see the attachments.

Will be appreciate for any answers and comments, why this happens.

P.S I live in Russia, so sorry if I have language mistakes, I'm studding English with passion in deed...

Last edited by ilnar; 06-07-2011 at 05:08 AM..
# 2  
Old 06-07-2011
sorry, forgot attachments
# 3  
Old 06-07-2011
Try with

--exclude=/home/dasha/my\\ songs
# 4  
Old 06-07-2011
Thanks for reply
I,ve tried this, but unfortunately, it was included in tar archive anyway, the code, that does changes from " " => "\\ " inside dir names is:

Code:
OLD_IFS="$IFS"
IFS=""
USER_EXCLUDE=`ls -d ${HOME_DIR}/*/*/`
USER_EXCLUDE=`echo $USER_EXCLUDE | sed s/" "/"\\\\\\\\\\\\\ "/g`
USER_EXCLUDE=`echo $USER_EXCLUDE | sed s/"\(.*\)\/"/"--exclude=\1"/g`
IFS=$OLD_IFS

It's required so many \\\\\\\\\\\..., to reflect "\\"
# 5  
Old 06-07-2011
If the value stored in the variable $EXCLUDE contains any IFS characters (by default, space, newline, and tab), after the shell expands $EXCLUDE into its value, it will split the resulting string into fields on those IFS characters. All the backslashes and quotes in the world within the value of $EXCLUDE won't stop that, since those backslashes and quotes are not in any way special to the shell (by the time field splitting begins, the parsing step that looks for quotes has completed).

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 06-10-2011
Thanks Alister,

I suppose there is no way to expand $EXCLUDE with correct meaning of "\ " Smilie I think this is kind of disadvantage of shell programming to traditional. There should be clear text manipulation(processing) mechanism...
# 7  
Old 06-10-2011
Quote:
Originally Posted by ilnar
[/CODE]It's required so many \\\\\\\\\\\..., to reflect "\\"
That's because you're using double-quotes, where the shell handles escaping itself and needs two to tell it to ignore it. Try single single-quotes, where escaping doesn't happen in the shell:
Code:
's/ /\\ /g' is equivalent to  "s/ /\\\ /g"

And you can single-quote just part of an expression too, the way you've been doing with double quotes.

I just remembered another trick: IFS=$'\n' This will cause the shell to split only on newlines when expanding variables -- spaces in directory names won't bother it at all. IFS is a special variable telling it how to expand variables and how to split when using read. This is the 'clear text processing mechanism' you wanted Smilie

Last edited by Corona688; 06-10-2011 at 12:46 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

2. Shell Programming and Scripting

How to make Shell script of using tar command?

Hi Guys, I have to make shell script means tar all files from path /home/admin and move to path /dis/wis/ Please help me. (5 Replies)
Discussion started by: aaditya321
5 Replies

3. Shell Programming and Scripting

Shell script to call Oracle archive backup script when file system reaches threshold value

Hello All, I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60% Need to identify the unique file system that reaches threshold value. ex: capacity ... (4 Replies)
Discussion started by: sasikanthdba
4 Replies

4. Shell Programming and Scripting

Can i use if else inside expect command in shell script?

hii,, I am trying to automate jira. during my scripting using bash script, in the terminal i got the terminal message like this: "Configure which ports JIRA will use. JIRA requires two TCP ports that are not being used by any other applications on this machine. The HTTP port is where you... (1 Reply)
Discussion started by: nithinfluent
1 Replies

5. Shell Programming and Scripting

Not the correct output, works fine via CLI, not inside the script.

Guys, I need you help please. The script below is not working correclty for checking via a awk/if statement . Can you tell me what i am doing wrong in the script code "if($1 == "$RETENTION_LEVEL") " Syntax RETENTION_LEVEL=`echo $LINE | cut -f2 -d" "` echo " ==============... (4 Replies)
Discussion started by: Junes
4 Replies

6. Shell Programming and Scripting

How to monitor a command inside shell script

Hi All, Is there any way to monitor a command inside shell script ? I have a script inside which I have a tar command which zips around 200GB data. tar zcvf $Bckp_Dir/$Box-BaseBackup-$Day.tar.gz * --exclude 'dbserver_logs/*' --exclude postmaster.pid --exclude 'pg_xlog/*' I want to... (3 Replies)
Discussion started by: sussus2326
3 Replies

7. AIX

Which is suitable Tar or Backup command????

hi all im making a script for backing up a specific filesystem that is an output of our DB13(SAP) into tape..which is around 40g + which is suitable tar or backup command i heard tar has limitations ..i heard it can only backup upto 2 gig??? is this ryt? and ill put this into cron. ... (6 Replies)
Discussion started by: redmanshogun
6 Replies

8. UNIX for Dummies Questions & Answers

how to get the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (1 Reply)
Discussion started by: kripssmart
1 Replies

9. UNIX for Dummies Questions & Answers

restoring backup using tar command

Please help me in resolving the issue. I have taken backup using the below command $ tar cvf - . |compress -> /opt/globusback2/needed_backups/apglsg.tar.Z I tried to restore the backup using the below command. $ zcat /opt/globusback2/needed_backups/apglsg.tar.Z | tar -xvf - ... (2 Replies)
Discussion started by: amirthraj_12
2 Replies

10. Shell Programming and Scripting

Using tar inside a shell script

Hi, I am using tar cvf inside a shell script to archive files. Is there an option to surpress any prompts which come up if the desired archive name already exists ? Thanks in Advance. Kas. (2 Replies)
Discussion started by: kas7225
2 Replies
Login or Register to Ask a Question