Friday, April 1, 2016
Linux Bash Shell Script File Executable check and Owner permission Removal
April 01, 2016
bash
,
beginner
,
code
,
executable check
,
explanation
,
file type check
,
function argument passing
,
function call
,
function parameters
,
if condition
,
linux
,
owner permission removal
,
script
,
shell
,
tutorial
How the code below works:
The code below checks if the given command line argument is a regular file. If it is then it checks if the file is executable. Finally if the file is executable the code removes the execution permission for the owner.Code To Check File Type & Remove Permission:
Save the code in a file named "FileCheckPermissionRemove.sh".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
removeExecutablePermission(){ | |
if [ -f $1 ]; then | |
#echo "regular file" | |
if [ -x $1 ]; then | |
#echo "remove permission" | |
chmod o-x $1 | |
fi | |
fi | |
} | |
removeExecutablePermission "$1" |
Running the Code:
Make sure to cd into that directory if you have not opened the terminal in that directory.Create a dummy shell script named "abc.sh" and set its permission to executable. Finally type the code in terminal.
bash FileCheckPermissionRemove.sh abc.sh
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment