I am trying to create a simple script to zip a list of files each into its own zip file. The files are big, so I a trying to send the to background using ampersand. It works as I can see the temporary files filling up and after some time the files are created, but issuing the 'jobs' command does not list the jobs. What am I doing wrong?
#!/bin/ksh
for file in $*;do
bash -c "zip -q $file.zip $file" &
done
As I said earlier, shell scripts execute in a subshell and the parent shell will not be able to list the jobs of a subshell. In order to use jobs
, the jobs need to be running in the same shell.
This can be achieved by source
-ing the file. Since your default shell is csh
the file should contain these lines according to the csh syntax
# not a script. no need for shebang
# sourcing this file **in csh** will
# start quiet zip jobs in the background
# for all files in the working dir (*)
foreach file in (*)
zip -q "$file.zip" "$file" &
end
Keeping this file in an easily accessible location and running source /path/to/file
will give you what you need.
This is the only way to do it in csh
for the following reasons:
jobs
will not be possiblecsh
does not support shell functionscsh
's foreach
syntaxchsh -s `which bash` $USER
bash
(or your shell of choice) to start a new shellecho $0
# executing this command appends a bash function named `zipAll` to ~/.bashrc
# modify according to your choice of shell
cat << 'EOF' >> ~/.bashrc
zipAll() {
for file in *; do
zip -q "$file.zip" "$file" &
done
}
EOF
zipAll
should be available from the next login onwards.bash
(or your shell of choice) to start a new shellbash
(or your shell of choicd) when you need this functionI do not know if this is a good solution. Hopefully someone will point out the ill-effects of it. Hopefully your organisation simply allows you to change the login shell
csh
, add a line to ~/.cshrc to start bash
(or your choice of shell)echo 'bash --login' >> ~/.cshrc
Confusion regarding zip usage was oversight on my part. Apologies.
NB: The syntax zip -q $file $file.zip
does not work with my version. But I retain it assuming that it works on OP's system
PS: The command that works with my version of zip
is zip -q $file.zip file