Managing File Permissions in Linux: How to Make ‘script.sh’ Executable with a Shell Script

Shell Scripting @ Freshers.in

File permissions in Linux are a crucial aspect of system security and functionality. They determine who can read, write, or execute a file. In this article, we will explore how to write a shell script that changes the permissions of a file named ‘script.sh’ to make it executable by the user.

Use your preferred text editor to create a new file. You can name it something like ‘change_permissions.sh.’ To create it using the nano text editor, run:

Inside ‘change_permissions.sh,’ write the following shell script:

#!/bin/bash
# Define the file for which you want to change permissions
file="script.sh"
# Check if the file exists
if [ -e "$file" ]; then
  # Change permissions to make the file executable by the user
  chmod +x "$file"
  echo "Permissions of '$file' have been changed to make it executable by the user."
else
  echo "File '$file' does not exist."
fi

This script sets the file variable to ‘script.sh,’ checks if the file exists, and if it does, it uses the chmod command to make it executable by the user.

Execute the script

./change_permissions.sh

Read more on Shell and Linux related articles

Other urls to refer

  1. PySpark Blogs
Author: user