Find the answer to your Linux question:
Results 1 to 6 of 6
i have just started out scripting and have some doubts , 1) How do i print the file access permission(FAP) for all the type of users ?? 2) how to ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    5

    Unhappy Bash shell scripting doubts

    i have just started out scripting and have some doubts ,

    1) How do i print the file access permission(FAP) for all the type of users ??

    2) how to search for files with same name in current directory ?

    3) how do i validate a blank user input ? like if user doesnt enter any values and presses Enter, what condition do i put in an If construct ?

    4) how to check what files were created today ? ls shows only the last modification date.

    thnx for reading the post.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    It's against the rules to post homework here.
    Google on "howto bash scripting"

    Regards

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Posts
    5
    Quote Originally Posted by Franklin52 View Post
    It's against the rules to post homework here.
    Google on "howto bash scripting"

    Regards
    this isnt a homework question. i found some good tutorials but i prefer practical advice on these questions.

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Ok, what have you tried so far?
    And what goes wrong?

    Regards

  5. #5
    Just Joined!
    Join Date
    Apr 2007
    Posts
    5
    Quote Originally Posted by Franklin52 View Post
    Ok, what have you tried so far?
    And what goes wrong?

    Regards
    well, by some research and trial n error i have solved most of the doubts to an extent ,

    1)for the FAP problem , i did this,

    Code:
    echo -n "Enter Filename : " ; read fname ;
    
    # check if file exists
    if [ -e $fname ]
    then
    	 
    #display fap for owner 
    
    	echo "Owner"
    
    	ls -l $fname|awk  '{print $1 }' |tee temp | cut -c2-4
    
    #display fap for group users
    	
    	echo "Group"
    
    	cat temp | cut -c5-7
    
    #display fap for other users
    
    	echo "Other"
    	
    	cat temp | cut -c8-10
    else
    	echo "FIle does not exist"
    fi
    the output is :

    Code:
    Owner 
    rwx
    Group
    r-x
    Others
    r--
    Now, this is an acceptable output , but i want to print the words "Read" "Write" "Execute" instead of 'rwx'. If it were only for the owner , i could use the test command and -r, -w, -x options but that doesnt work for Group and Other users, how do i do that ?

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    An example:

    Code:
    #!/bin/sh
    
    Permission()
    {
    
    for((i=0; i<3; i++))
      do
      case ${perm:i:1} in
        "r")
          printf "Read\t"
          ;;
        "w")
          printf "Write\t"
          ;;
        "x")
          printf "Execute\t"
          ;;
        esac
      done
    
      echo
    
    }
    
    echo -n "Enter Filename : " ; read fname
    
    if [ ! -e $fname ] ; then
      echo File $fname does not exist
      exit 1
    fi
    
    fileperm=`ls -l $fname`
    
    # I don't use a loop here to show the use of substrings
    # The function Permission is called 3 times
    
    echo Owner
    perm=${fileperm:1:3}
    Permission
    
    echo Group
    perm=${fileperm:4:3}
    Permission
    
    echo Others
    perm=${fileperm:7:3}
    Permission
    
    exit 0
    I've use a function to print out the permissions.
    It's not an easy example for a beginner but spend some time to find out how it works, go here for some tutorials:

    http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
    http://tldp.org/LDP/abs/html

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...