Function to Edit an existing file
edit_existing_file() {
echo “Enter the full path or name of the file to edit:”
read filepath
if [ -f “$filepath” ]; then
# Display file contents with line numbers
nl “$filepath”
cat > “$filepath.tmp” # Create temporary file to store new text
mv “$filepath.tmp” “$filepath” # Replace original file with the one containing new text
echo “File edited.”
else
echo “File not found.”
fi
}
Function to Edit an existing file
edit_existing_file() {
echo “Enter the name of the file or the full file path to edit:”
read filename
if [ -f “$filename” ]; then
# Display file contents with line numbers
nl “$filename”
echo “Enter the new content below. Press Ctrl+D when finished:”
cat > “$filename.tmp” # Create temporary file to store new text
mv “$filename.tmp” “$filename” # Replace original file with the one containing new text
echo “File edited.”
else
echo “File not found.”
fi
}
justmaxcz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.