Dear all experts,
I have a script written to compress a list of files, during compressing, some of the files are having same name. When the compressing started, the same name file will be prompted with message whether to overwrite the old file. I need to enter "y" to continue.
Is there any way I can put into the script so that it will answer "y" for each of the prompted question?
In DOS, as I know can put a prompt off, but I do not know what should I put on Shell Script.
Thanks.
If you refer to tar
then this might help:
tar --help|grep -i over
Overwrite control:
--no-overwrite-dir preserve metadata of existing directories
--overwrite overwrite existing files when extracting
--overwrite-dir overwrite metadata of existing directories when
silently skip over them
-U, --unlink-first remove each file prior to extracting over it
--suffix=STRING backup before removal, override usual suffix ('~'
unless overridden by environment variable
Dear sea,
I am not referring to tar command. I refer to compress command.
For example, I have a file name "aaaa" and I would like to compress it. I will run command "compress aaaa" and it will become aaaa.Z
After that I received another aaaa file again. This time when I perform compress aaaa, the system will prompt me a message as
Do you wish to write over the existing file aaaa.Z?
I have to enter "y" to overwrite the original file.
I would like to know how can I skip the answering "y" in my script so that the above prompt will not appear?
Did you check the -f
option ?
man compress
sea, what you quoted from is a Linux man page. This is the AIX board and the AIX tar works quite differently - POSIX-compatibly, that is.
Even if sea's suggestion was faulty his intention was right: read the man page (of compress
, instead of GNU-tar) to find the following:
-f or -F
Forces compression. The -f and -F flags are interchangeable.
Overwrites the File.Z file if it already exists.
How to use man
As you mentioned DOS i suppose you are not familiar with UNIX in general or AIX in specific. Here is a general advice: if you do not know how to use/configure a command you can always enter
man command
to get the "man[ual] page" of command. This works even for man
itself, so man man
will tell you how to use man
. The works not only for commands but also standard configuration files. Enter, for instance,
man filesystems
to get information about the file /etc/filesystems
, which denotes all the mountable filesystems in a system.
(Tip: the paginating program used in man
is more
which navigation keys match the ones of vi
. Scroll down a line with "j", scroll up a line with "k", etc..)
If you are not sure which command to use you can search all the man pages for a specific term with the man -k keyword
(or its more eponymic alias apropos keyword
) command, which will list all the man pages containing keyword
.
For instance:
# man -k users
bellmail(1) - Sends messagesto system users and displays messages from system users.
chargefee(1) - Charges endusers for the computer resources they use.
chpasswd(1) - Changes password for users.
comsat(1) - Notifies usersof incoming mail.
custom(1) - Enables usersto customize X applications.
[...]
# apropos users
bellmail(1) - Sends messagesto system users and displays messages from system users.
chargefee(1) - Charges endusers for the computer resources they use.
chpasswd(1) - Changes password for users.
comsat(1) - Notifies usersof incoming mail.
custom(1) - Enables usersto customize X applications.
[...]
I hope this helps.
bakunin
TQVM for the parameter "-f". I just keep on digging how to send in a "y" for whatever return on screen. Never in my mind tho of using compress function's parameter.
Thanks.
---------- Post updated at 05:34 PM ---------- Previous update was at 05:29 PM ----------
Dear Bakunin,
Thanks for your detail and helpful hint.
I do know how to use AIX's man. I not familiar with Linux tar. Now only I know tar is something like man in unix.
I was digging the way how to pass a "y" into anything pop up on screen. Never tho of checking function compress's ability.
Anyway, thanks a lot.
@ bakjunin: Yes sorry, was just looking at the threads title.
@ kwliew999: No, man
is same for both, but tar
is like compress
.
Sorry for the irritation.
But either way, these kind of commands usualy provide arguments to overwrite (or not) any possible conflicting files.
And terminal commands usualy come along with (sometimes just either one of) these arguments:
-h
--h
-help
--help
Which i prefer on many occasions, since i can grep
for keywords, which i'm not aware to do in manpages.
Helps alot to get quicker to the helpfull 'part'.
Hope this helps
As you might need this for other occasions here is how you do it:
Short introduction to I/O redirection
You can picture any process in UNIX to be like a garden hose: something goes in on top, something goes out on the bottom. You can daisy-chain processes in a pipeline with the "|"-symbol, which simply means "connect the output of one process with the input of the other".
Now suppose you want to produce a "y". You could use (try out the following out, it is all non-descructive):
print - "y"
So this is a process which takes nothing in and sends something (a "y") to its output. Let us construct a process processing some input (we could use your compress-process, but we want something we can watch more closely):
#! /bin/ksh
typeset input=""
while : ; do
input=""
echo "enter something: " ; read input
case $input in
y)
echo "you entered a 'y'. Exiting...."
exit
;;
*)
echo "you entered '$input'. Enter 'y' to exit."
;;
esac
done
Save this to a file "logger.ksh" and make it executable. Try it out on its own (it can be tricked by keyboard artistics like CTRL-C, but enter something "normal" and it does what you would expect - exit on "y" and start over on anything else).
Now call
print - "y\n" | ./logger.ksh
and you will notice that this is the same as if you would have entered it by keyboard. Lets do another test. In case you wonder about the "\n" - these are just codes for the ENTER-key on your keyboard:
# print - "a\nb\ny\n" | ./logger.ksh
enter something:
you entered 'a'. Enter 'y' to exit.
enter something:
you entered 'b'. Enter 'y' to exit.
enter something:
you entered a 'y'. Exiting....
Per default the input of a process is taken from the keyboard - like the shell (which is just another process), which listens there and takes up what you type. But by redirecting the input you can make the process take its input from any other source, just like you can make it put its output to somewhere else (like a file) instead of the screen it would normally output to. You can even use a file for input:
process < /path/to/some/file
To stay in the garden-hose picture i used above: you plug the respective end of the hose to some device, which can provide/store something to put through the hose - files, devices (like keyboards for input, screens for output) or other processes.
I hope this helps.
bakunin