LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Permission problem (https://www.linuxquestions.org/questions/programming-9/permission-problem-532432/)

kalyanofb 02-25-2007 11:49 PM

Permission problem
 
Hai,

I wrote a shell script in which i am using lex output file which reads the input file and generates an output file removing comments inside the input file.

While running this script, it gives some permission problem while reading a input file, to avoid this i want to get the permission of the input file and stored in some variable. Then change input file permission to read mode. After execution of lex, i will reset the permission to the input file.

How i will do this?

pls. help me.

Dark_Helmet 02-26-2007 12:51 AM

This works for basic permissions (rwx). I don't know if it will choke on special bits (sticky bit, suid, and sgid). If it does, I trust this is enough of a start for you to figure out how to handle them.

Code:

#!/bin/bash

target_file=test_file

original_perms=$( ls -l ${target_file} | cut -c 2-10 )

chmod_mode="u=$(echo ${original_perms} | cut -c 1-3 | sed 's/-//g'),\
g=$(echo ${original_perms} | cut -c 4-6 | sed 's/-//g'),\
o=${echo ${original_perms} | cut -c 7-9 | sed 's/-//g')"

echo "  Original permissions: ${original_perms}"
echo "      chmod mode string: ${chmod_mode}"
echo
echo "Changing permissions to: 777"

chmod 777 ${target_file}

echo "---Displaying file listing---"
ls -l ${target_file}

echo "Changing permissions back to original settings"
chmod ${chmod_mode} ${target_file}

echo "---Displaying file listing---"
ls -l ${target_file}

exit 0



All times are GMT -5. The time now is 06:35 PM.