shell scripting question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keyvanrahmadi
    New Member
    • Feb 2007
    • 57

    shell scripting question

    I have a course work questio which i like some input on, i have achieved the desired out put but the main problem is the script should be 15 lines and mine is 20 :(

    That said i know it can be done in far less using sed and awk, but cant use them at all for this question. can you guys please read through my script and give me suggestions please. i have tried two scripts:

    FIRST SCRIPT

    Code:
    #!/bin/bas
    int=1
    user=$(ls -l $1 | cut -d " " -f3)
    echo "      READ WRITE EXECUTE"
    while [ $int -le 9 ]
    do
    int=$[$int+1]
    case "$int" in
    2)echo -n "owner $user " ;;
    5)echo
      echo -n "group      " ;;
    8)echo
      echo -n "everybody  " ;;
    esac
    perm=$(ls -l $1 | cut -c$int)
    case "$perm" in
    -)echo  -n "no " ;;
    *)echo -n "yes " ;;
    esac
    done
    the out put is as follow:

    READ WRITE EXECUTE
    owner keyvan.ahmadi yes yes no
    group yes no no
    everybody yes no no

    the only problem with above script as i already mentioned is, its 20 lines and it needs to be 15 lines.

    SCRIPT 2

    Code:
    #!/bin/bash
    int=1
    for perm in `ls -l $1|cut -c2, 3,4,5,6,7,8,9,10 --output-delimiter " "`
    do
     case "$int" in
      1)echo -n -e "READ WRITE EXECUTE\nowner `ls -l $1 | cut -d " " -f5` " ;;
      4)echo -n -e "\ngroup `ls -l $1 | cut -d " " -f4` " ;;
      7)echo -n -e "\neverybody  " ;;
     esac
     case "$perm" in
      -)echo  -n   "no  " ;;
      *)echo -n  "yes " ;;
     esac
    done
    echo
    Now the second script has two problems

    1- well it dosent work :) and when run it gives the following output:

    READ WRITE EXECUTE
    owner 52 yes

    2- its 16 lines even if it worked which it dosent :(

    As i have said before i have managed to do it using sed & awk:

    Code:
    #!/bin/bash
    
    if [ -f $1 ]
    then
    ls -l $1 | sed -e '1,9s/x/YES /g;1,9s/w/YES /g;1,9s/-/NO /g;1,9s/r/YES /g' | awk -F' ' '{print "\t\t\tREAD\tWRITE\tEXECUTE\nOWNER "$12"\t"$2"\t"$3"\t"$4"\nGROUP "$13"\t\t"$5"\t"$6"\t"$7"\nEVERYBODY\t\t"$8"\t"$9"\t"$10}' > Report
    else
    :
     echo "Cannot find file."
    fi
    But i cant use it, beacuse my lecturer throw a temper tantrum at me and look at me as i have just killed his fav pet or something.

    So any help guys & gals will be greatly appriciated.

    keyvan
  • coaxfiber
    New Member
    • Mar 2007
    • 60

    #2
    Is your shell bash?

    Comment

    • keyvanrahmadi
      New Member
      • Feb 2007
      • 57

      #3
      Originally posted by coaxfiber
      Is your shell bash?
      its #!/bin/bash so the answer to your question is yes mate.

      keyvan

      Comment

      • keyvanrahmadi
        New Member
        • Feb 2007
        • 57

        #4
        SCRIPT 2 correct code is as follow:

        Code:
        #!/bin/bash
        int=1
        for perm in `ls -l $1|cut -c2-10 --output-delimiter " "`
        do
         case "$int" in
          1)echo -n -e "READ WRITE EXECUTE\nowner `ls -l $1 | cut -d " " -f3` " ;;
          4)echo -n -e "\ngroup `ls -l $1 | cut -d " " -f4` " ;;
          7)echo -n -e "\neverybody  " ;;
        esac
         case "$perm" in
          -)echo  -n   "no  " ;;
          *)echo -n  "yes " ;;
         esac
        done
        echo
        i had -f5 rather than -f3 in first line of echo. but th eproblem is still the same.

        keyvan

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          15 lines? if you bring all your lines into one line using ";" it would be even better, but you aren't going to do that right.?
          What's wrong with more than 15 lines, if its more readable to everyone ? anyway here's a crude implementation in awk:
          Code:
          ls -l "file" |  awk 'BEGIN { 
                            owner["r"] = "read";owner["w"] = "write";owner["x"]= "execute";owner["-"]="No"
                            group["r"] = "read";group["w"] = "write";group["x"]="execute";group["-"]="No"
                            every["r"] = "read";every["w"] = "write";every["x"]="execute";every["-"]="No"
                            printf "%-8s%-15s%-15s%-15s\n", "Field","READ" ,"WRITE","EXECUTE"                  
                            }                
                          { perm = $1; own=$3 }
                          END { FS=""; n = split(perm,permarray)
                              printf "%-8s",own
                              for (i=2;i<=4;i++) printf "%-15s"  , owner[permarray[i]];
                              printf "\n%-8s" , "group" 
                              for (i=5;i<=7;i++) printf "%-15s", group[permarray[i]];
                              printf "\n%-8s" , "All" 
                              for (i=8;i<=10;i++) printf "%-15s", every[permarray[i]]; print ""
                          }'
          output:
          Code:
          # ./test.sh
          Field   READ           WRITE          EXECUTE
          root    read           write          No
          group   read           No             No
          All     read           No             No

          Comment

          Working...