General TC > Programming & Scripting - Unofficial
Help with my bash script
ferran:
Hello everybody
This is my first shell script in TC 8)
I want to make a script to edit text files avoiding all the steps to enter them (enter as sudo, change permissions, edit, change permissions again and exit).
The problem is that the script doesn't execute anything and I suspect that is because BusyBox doesn't include the stat command).
--- Code: ---#!/bin/bash
#####
#
# Bash script to edit any text file wih the GUI editor
#
# (At final It will restore the original permissions of the file selected)
#
# Name: editfile.sh
# To run: ./editfile.sh <file>
# Autor: Ferran
#
#####
## Get the argument of selected file from commands line ##
argFile = $1
## Get the permissions of the file and save it
getPerm = "$(stat -c '%a' $argFile)"
## Set -temporal- permissions to open the file
sudo chmod 777 $argFile
## Execute the select editor to open the file
editor $argFile
## When the editor is closed we'll restore the original permissions
chmod $getPerm $argFile
## Exit of sudo mode
exit
--- End code ---
What can I do? :-[
Rich:
Hi ferran
stat is provided by coreutils.tcz. bash is not part of base so you need bash.tcz. You also need to make your script executable:
--- Code: ---chmod 755 MyScript
--- End code ---
ferran:
Ok Rich. I get this extension and I'll try again.
Greg Erskine:
Rather then loading bash.tcz, change #!/bin/bash to #!/bin/sh
Do you really need bash? No.
Get rid of the spaces around the "=". For example, argFile = $1 should be argFile=$1
When you edit files using sudo, you should be able edit them without changing permissions. I find having to change permissions is a rarity once the permissions of a files are setup correctly in the first place.
ferran:
Greg
Everytime I need edit f.i. bootlocal.sh filetool.lst or others, usually its permissions are in read or read-execute mode only. So to edit them I need to change it.
Note: I just changed #!/bin/sh
Apart of that:
the program recognizes now the command stat and the program works well (I can open the file I want) but in the terminal shows an error:
--- Quote ---tc@box:~/docs$ ./editfile.sh pla_de_comptes.txt
the file is pla_de_comptes.txt
./editfile.sh: line 23: getPerm: not found
the chmod is
chmod: missing operand after ‘pla_de_comptes.txt’
Try 'chmod --help' for more information.
--- End quote ---
Navigation
[0] Message Index
[#] Next page
Go to full version