0

hi everyone i did this script :

#!/bin/bash


if  [ $# -ne 1 ] ; 

        then 

            echo "Errore, puoi immettere solo un argomento"

                   exit 


elif ! [ -d  $1 ] ; 

       then 

           echo "Errore, "$1" non è una directory"

                  exit 

                           fi

if  [ -e $1.tar.gz ] ; 

   then 

        echo "Questo archivio è già esistente,vuoi sovrascriverlo (s/n)? "

read opt 
case $opt in 

s) 
    rm -rf $1.tar.gz

        tar -czvf $1.tar.gz $1


     if  ! [ $? -eq 0  ] ;

       then 
            clear
            echo "Errore comando non andato a buon fine"



         else

           clear

          echo "L'archvio è stato sovrascritto, questo è il suo contenuto : "  

             tar ztvf $1.tar.gz
                  fi

     exit   
               ;;


n) 
         exit  ;;

*) 
    echo "Comando non valido " ;;

        esac

  else  

      tar -czvf $1.tar.gz $1   

     if  ! [ $? -eq 0  ] ;

       then 
            clear
            echo "Errore comando non andato a buon fine"

      else     
           clear 
            echo "Archvio creato con successo,il contenuto è : "

                tar ztvf $1.tar.gz

                     fi
                           fi

I don't understand why on line 38, referring to if [$? -ne 0] after removing and creating the .tar.gz archive, the terminal gives me "command not found", among other things, I used the same instruction inside the if of some line of code later ... can someone explain to me where I am wrong?

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • 1
    I don't see that in the code you posted. But if you're missing the spaces in `[ $? -ne 0 ]` then you'll get that error. – Barmar Apr 13 '20 at 20:51
  • 1
    A tip: Paste bash scripts into https://www.shellcheck.net/ to get helpful tips about how to improve it. This would also have pointed out your mistake here. – CherryDT Apr 13 '20 at 20:54
  • You have no "-ne 0" in your code. Please double check your script example. and maybe do some friendly formatting, I believe you miss something because a lot of extra spaces/empty lines – Saboteur Apr 13 '20 at 21:49
  • @Barmar, I don't think the issue is with spaces, I believe the issue related to wrong usage of ascention mark. "!" is a part of test command, and should be inside brackets. Try to rewrite this: elif ! [ -d $1 ] ; to this: elif [ ! -d $1 ] – Saboteur Apr 13 '20 at 21:52
  • @Saboteur It's also part of the `if` command, it negates the exit status of the command. – Barmar Apr 13 '20 at 21:55
  • 1
    @Saboteur He says the error is happening on the line `if [$? -ne 0]` which is missing spaces. It's not in the posted script, he must have posted something different from what he's running. – Barmar Apr 13 '20 at 21:56
  • sorry, the command I meant was " if! [$? -eq 0] " after "rm -rf $ 1.tar.gz" and "tar -czvf $ 1.tar.gz $ 1". The script must simply say, using the "if" , if the last command given has worked or not ... what I don't understand is that this command ( if ! [ $? -eq 0 ] ; ) I use it 2 times in exactly the same way, the first "if" gives an error, the second works without problems... – checcasio Apr 14 '20 at 21:20

0 Answers0