bad interpreter when running script

Hi All,

I'm not confortable in writing script, can someone can help me, when I run that script below i found this error code : -bash: ./script.sh: /bin/sh.: bad interpreter:

Here is the script

for i in *

        x=${i##*.}
        z=$(perl -e 'print time;')
        t=$(echo $z-$x|bc)
        r=test_file.$x


        if ((t>>86400))
        then tar -cvf $r.tar $r
                echo $i
                echo $z
                echo $x
                echo $t
                echo $r
                fi
        done

Regards

few things to check

  1. if /bin/sh is available
 ls -l /bin/sh 
  1. have set eXecute permissions
chmod 755 script.sh

Done but same error,

In my opinion there is something wrong with the test : "if ((t>>86400))"

There are a few issues, starting with the fact that you didn't post the whole script. Especially the "bad interpreter" problem indicates an error in the "shebang" line defining the interpreter, and that line is missing altogether.

Second, the quotes if condition: are you attempting to say "much larger than", or a shift-right by 86400?

The traduction is, after bad interpreter : missing file or directory.

  • I'm get confuse, the expression ((t>>86400)) try to put the value of "t" into 86400 file, but my wish is to test "t" to know that if "t" is greater than 86400.
    Apologize, I'm french.
    Regards.

Can you please post the complete script, including the very first line, which should look like this:

#!/bin/sh

The comparison fails for a very simple reason: the brackets you're using (>>) are used for redirection, not comparison. The proper comparison would look like this:

if [ $t -gt 86400 ]

This is all code, before modification :

#!/bin/sh
 
for i in /opt/product/arsystem/test/    
do file "$i"  
    x=${i##*.}
    z=${perl -e 'print time;}
    r=LicenseReport.txt.$x    
     t=$(echo $z-$x|bc)    
    if ((t>86400))
    then tar -cvf $r.tar $r
    
        echo $i;
         echo $z; 
         echo $x;
         echo $t; 
         echo $r;
    else
    fi
     done

---------- Post updated at 11:43 AM ---------- Previous update was at 11:39 AM ----------

Actually the error become : Syntax error line 19 unexpected end of file,
When I try to run the script in the command line it's work fine, after put in text file .sh everything goes wrong.

#!/bin/sh

# This would run the loop exactly once, with i="/opt/product/arsystem/test/".
# for i in /opt/product/arsystem/test/    

# I think you meant this, to match all files in that dir
for i in /opt/product/arsystem/test/*
do
    # just a sanity check.  Don't run file on directories.
    [ -d "$i" ] && continue
    # newline here
    file "$i"  
    x=${i##*.}
    # wrong kind of brackets, and you missed one '
    # z=${perl -e 'print time;}
    z=$(perl -e 'print time')
    r=LicenseReport.txt.$x    
     t=$(echo $z-$x|bc)    
    if ((t>86400))
    then
        # newline here
        tar -cvf $r.tar $r
    
        echo $i;
         echo $z; 
         echo $x;
         echo $t; 
         echo $r;
    else
        # can't leave a branch empty.
        # Use : whenever you need to do nothing.
        :
    fi
done

There's all kinds of ways to optimize this script too, if you're interested. Lots of unnecessary calls to perl and bc can be streamlined.

You're also using some shell-specific things like the double bracket (( )) operator. If you want to use these, you should make the first line #!/bin/bash or #!/bin/ksh (depending on which you're using) so the script doesn't mysteriously break down when you move it to another system.

Not sure this line is syntaxically correct:

z=${perl -e 'print time;}

Maybe you meant this ?

z=$(perl -e 'print time')

That's I wrote, fi after done work's better, but we still have error :

 - ./script.sh: erreur de syntaxe ligne 18: `t=$' inattendue
 - ./script.sh: syntax error line 18: `t=$' unexpected.

I working on it

Regards :o

--------------------------------------------------------------------------

#!/bin/sh

# This would run the loop exactly once, with i="/opt/product/arsystem/test/".
# for i in /opt/product/arsystem/test/    

# I think you meant this, to match all files in that dir
for i in /opt/product/arsystem/test/*
do
    # just a sanity check.  Don't run file on directories.
    #[ -d "$i" ] && continue
    # newline here
    file "$i"  
    x=${i##*.}
    # wrong kind of brackets, and you missed one '
    z=${perl -e 'print time'}
    # z=$(perl -e 'print time')
    r=LicenseReport.txt.$x    
    t=$(echo $z-$x|bc)    
    if ((t>86400))
    then
        # newline here
        tar -cvf $r.tar $r
    
        echo $i;
         echo $z; 
         echo $x;
         echo $t; 
         echo $r;
    else
        # can't leave a branch empty.
        # Use : whenever you need to do nothing.
        :
    done    
    fi

---------- Post updated at 04:41 AM ---------- Previous update was at 04:09 AM ----------

With the script below we have the error :

/opt/product/arsystem/test/LicenseReport.txt.1298111407:        commandes
./script.sh: substitution incorrecte (bad substitution)
#!/bin/sh

# This would run the loop exactly once, with i="/opt/product/arsystem/test/".
# for i in /opt/product/arsystem/test/    

# I think you meant this, to match all files in that dir
for i in /opt/product/arsystem/test/*
do
    # just a sanity check.  Don't run file on directories.
    [ -d "$i" ] && continue
    # newline here
    file "$i"  
    x=${i##*.}
        echo $i;
         echo $z; 
         echo $x;
         echo $t; 
         echo $r;
    # wrong kind of brackets, and you missed one '
    z=${perl -e 'print time'}
    # z=$(perl -e 'print time')
    r=LicenseReport.txt.$x    
    t=${echo $z-$x|bc}    
    if [ "$t" -gt "86400" ]
    then
        # newline here
        tar -cvf $r.tar $r
    

    else
        # can't leave a branch empty.
        # Use : whenever you need to do nothing.
        :
    fi    
   done

---------- Post updated at 05:01 AM ---------- Previous update was at 04:41 AM ----------

Find something wrong, I put echo $i before x=${i##*.}, then he show me the value of i like that :

/opt/product/arsystem/test/LicenseReport.txt.1298111407
#!/bin/sh
for i in /opt/product/arsystem/test/*
do
    # just a sanity check.  Don't run file on directories.
    [ -d "$i" ] && continue
    # newline here
    file "$i"
    x=${i##*.}
        echo $i;
         echo $z;
         echo $x;
         echo $t;
         echo $r;
    z=$(perl -e 'print time')
    r=LicenseReport.txt.$x
    t=$(echo $z-$x|bc)
    if [ "$t" -gt "86400" ]
    then
        # newline here
        tar -cvf $r.tar /opt/product/arsystem/test/$r
    fi
   done

I've simplified the code and made some arrangements:
For the first two red lines, you needed to use parenthesis and not brackets (brackets are for variable substitution)
The third red line is here because the tar command need to have correct paths in order to behave properly. The error you'll have in this case is :

tar: LicenseReport.txt.1298111407: Cannot stat: No such file or directory

You copied the comment telling you you had the wrong brackets, but still didn't use the right brackets... :confused: Maybe {} and () look extremely similar in some fonts. The brackets for variable names are shift-[ and shift-], the brackets for enclosing statements are shift-9 and shift-0.

If you have an AZERTY keyboard, the bracket and parenthesis are done with different keystrokes :wink:
AltGr-4 for {
AltGr-+ for }