开发者

Can tar -xvf be used in a If statement to return true or false

开发者 https://www.devze.com 2023-03-23 15:51 出处:网络
I know I might sound weird but I wa开发者_JAVA技巧s wondering if I could do a if [ tar -xvf ] on some file and that will return me true or false .

I know I might sound weird but I wa开发者_JAVA技巧s wondering if I could do a if [ tar -xvf ] on some file and that will return me true or false .

I need to write something to a file if the tarring was successful and if not then something else is written on the same file.

something like

if[ find . -maxdepth 1 -name "*.tar" -exec tar -xvf {} ] ; then
echo " Untar Successful"> test.txt
else
echo "Untar Not Successful"> test.txt

Please let me know if there is something I can do to implement this


EXIT STATUS
     The tar utility exits 0 on success, and >0 if an error occurs.

You can check error like

if [ $? -ne 0 ]; then

Where $? is the most recent foreground pipeline exit status.


You cannot reliably use the status of find to determine whether the commands it executes executed successfully. The POSIX definition of find says:

find

EXIT STATUS

The following exit values shall be returned:

0 All path operands were traversed successfully.

>0 An error occurred.

This is different from saying anything about the exit statuses of any executed commands.

The xargs utility does provide the information you need. POSIX says:

xargs

EXIT STATUS

The following exit values shall be returned:

0 All invocations of utility returned exit status zero.

1-125 A command line meeting the specified requirements could not be assembled, one or more of the invocations of utility returned a non-zero exit status, or some other error occurred.

126 The utility specified by utility was found but could not be invoked.

127 The utility specified by utility could not be found.

So, you could use:

if find . -maxdepth 1 -name "*.tar" -print0 | xargs -0-L 1 tar -xf
then echo "Untar Successful"
else echo "Untar Not Successful"
fi > test.txt

Note that this only uses one redirection. It tests the exit status of the pipeline, which is the exit status of the last command in the pipeline, which is the exit status of xargs, which tells you whether it worked or not.

The -print0 and -0 options are GNU extensions to the POSIX standard. As long as your filenames do not contain spaces or newlines or tabs, you can safely use -print (and no corresponding argument to xargs) instead.


You can do:

find ... -exec sh -c 'tar ... && echo success || echo failure'

or

find ... -exec sh -c 'if tar ...; then echo success; else echo failure; fi'

(This emits a message for every file, and you can include the path of the tar file by putting {} in the echoed message. If you want to make one report at the end stating that all of the tars were successful, you could do something like:

if find ... -exec sh -c 'tar ... && echo success || echo failure' |
  grep failure > /dev/null; then
    echo "untar failed"
else
    echo "untar successful"
fi

but that seems less useful to me.


You can definitely do "tar -tf" on tar files. Options may very according to the extensions you have. You can also add options "v" for verbose.

tar -Jtf "ABC.tar.xz"
tar -jtf "ABC.tar.bzip2"
tar -tf "ABC.tar"

You can even find list of files.

tar -tf "ABC.tar" "./ABC/hello.c" >/dev/null 2>&1

If file hello.c is present in zip file, it will return success(0) otherwise it will return error code.

0

精彩评论

暂无评论...
验证码 换一张
取 消