5 Replies Latest reply: Jun 5, 2014 2:55 PM by Paul M. RSS

    Error in my simple shell script

    1614316

      I am trying to write the shell script for changing the permissions of file to 774  Octal format(only if the present permission of file is 777).I tried the Below one and i am getting the error

       

      cat shell2.log

       

      #!/bin/bash

      FILES=/u01/app/oracle/*

      for f in $FILES

      do

        echo "Processing $f file..."

        a="stat -c "%a %n" $f|awk '{print $1}'"   -->Trying to get the octal permissions of the file here i think i am getting the error at this line

        if [ "$a" == '777' ]; then

        chmod 774 $f

        cat $f

        else

        echo "file is already having proper privilege"

        fi

        done

       

      Tried to run the shell script

      ./shell2.log

       

      Processing /u01/app/oracle/shell.log file...

      ./shell2.log: line 7: fg: %n /u01/app/oracle/shell.log|awk '{print }': no such job

      file is already having proper privilege

      Processing /u01/app/oracle/test1.log file...

      ./shell2.log: line 7: fg: %n  /u01/app/oracle/test1.log|awk '{print }': no such job

      file is already having proper privilege

      Processing /u01/app/oracle/test.log file...

      ./shell2.log: line 7: fg: %n //u01/app/oraclei/test.log|awk '{print }': no such job

      file is already having proper privilege

       

      Can someone help me on this Please?

       

      Thanks,

      Bhavani.

        • 1. Re: Error in my simple shell script
          Catch 22

          It might work if you replace:

           

          a="stat -c "%a %n" $f|awk '{print $1}'"

           

          with

           

          a=$(stat -c %a $f)

          or

          a=`stat -c %a $f`


          However, for what you are trying to accomplish, the following single command might be more suitable and also change files in sub-directories.


          find ./ -type f -perm 777 -exec chmod 774 {} \;


          Whether is is actually a good idea to apply to an Oracle software installation is a different question though. I hope you know what you are doing.

          • 2. Re: Error in my simple shell script
            1614316

            @Dude

             

            Thanks for the above reply.

             

            However, for what you are trying to accomplish, the following single command might be more suitable and also change files in sub-directories.


            find ./ -type f -perm 777 -exec chmod 774 {} \


            The above command works fine for the files only which are having 777 permissions.Do you know how do i get only others(World) permissions(excluding user,group permissions).The actual requirement is  the files which are having world write privileges I will have to revoke them(Here the business is not caring about user,group privileges,only caring about world write privileges).

             

            Do you think umask will fit into this requirement?

             

            Thanks,

            Bhavani.

            • 3. Re: Error in my simple shell script
              Paul M.

              The actual requirement is  the files which are having world write privileges I will have to revoke them(Here the business is not caring about user,group privileges,only caring about world write privileges).

               

              Do you think umask will fit into this requirement?

              umask only works for newly created files. For already present files you may try

               

              find . -type f -perm -o+w -exec chmod o-w {} \;
              • 4. Re: Error in my simple shell script
                1614316

                Hello Paul,

                Thank you so much for your help.It helped me.Can you recommend me the good books for shell scripting for beginners.

                 

                Thanks,

                Bhavani.

                • 5. Re: Error in my simple shell script
                  Paul M.
                  Can you recommend me the good books for shell scripting for beginners.

                  Did you try googling ? Personally I can't say much, since I've always (and only) used the online help (man <command>).