LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-11-2009, 12:35 PM   #1
int0x80
Member
 
Registered: Sep 2002
Posts: 310

Rep: Reputation: Disabled
Question Bash background (&) with control operators (&&)


I have a set of files to copy and decompress, and want to do these operations concurrently with a script.

Manually it would be something like:

Code:
cp /src/file1.gz /dest/ & && gzip -d /dest/file1.gz &
cp /src/file2.gz /dest/ & && gzip -d /dest/file2.gz &
cp /src/file3.gz /dest/ & && gzip -d /dest/file3.gz &
The single & is intended to background the processes, while the && is intended to execute the gzip process if and only if the cp completes successfully.

My script is:

Code:
#!/bin/bash
FILES="*file$(date +"%m%d")*.gz"
DIR_SRC="/src"
DIR_DEST="/dest"

if [ -d $DIR_SRC ]; then
    for f in $(ls $DIR_SRC/$FILES); do 
        logger -s "cp_incoming.sh: processing $f";
        cp $f $DIR_DEST & && gzip -d $DIR_DEST/$(basename $f) &; 
    done
else
    logger -s "cp_incoming.sh: $DIR_SRC was not found - exiting"
    exit 1
fi

exit 0
When I run it, bash gets angry with the following error:

Code:
$ ./cp_incoming.sh 
./cp_incoming.sh: line 9: syntax error near unexpected token `&&'
./cp_incoming.sh: line 9: `        cp $f $DIR_DEST & && gzip -d $DIR_DEST/$(basename $f) &; '
So what is the proper syntax to accomplish this?

TIA
 
Old 02-11-2009, 12:54 PM   #2
int0x80
Member
 
Registered: Sep 2002
Posts: 310

Original Poster
Rep: Reputation: Disabled
Figured it out:

Code:
#!/bin/bash
FILES="*file$(date +"%m%d")*.gz"
DIR_SRC="/src"
DIR_DEST="/dest"

if [ -d $DIR_SRC ]; then
    for f in $(ls $DIR_SRC/$FILES); do 
        logger -s "cp_incoming.sh: processing $f"
        cp $f $DIR_DEST && gzip -d $DIR_DEST/$(basename $f) &
    done
else
    logger -s "cp_incoming.sh: $DIR_SRC was not found - exiting"
    exit 1
fi

exit 0
I just needed to treat the whole cp && gzip operation as one process to be backgrounded. Removing the semicolons at the end of the lines int he for loop also helped.
 
  


Reply

Tags
background, bash, control



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
zsnes && logitech dual action gamepad && dpad woes John5788 Linux - Games 5 10-29-2008 09:56 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 12:39 PM

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

All times are GMT -5. The time now is 01:33 AM.

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