WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Help with my bash script  (Read 3914 times)

Offline ferran

  • Full Member
  • ***
  • Posts: 159
Help with my bash script
« on: April 06, 2020, 04:56:45 PM »
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: [Select]
#!/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


What can I do?  :-[
« Last Edit: April 06, 2020, 05:15:49 PM by ferran »
TC CorePlus v.11.1 i686 & lots of coffe

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Help with my bash script
« Reply #1 on: April 06, 2020, 05:06:24 PM »
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: [Select]
chmod 755 MyScript

Offline ferran

  • Full Member
  • ***
  • Posts: 159
Re: Help with my bash script
« Reply #2 on: April 06, 2020, 05:14:01 PM »
Ok Rich. I get this extension and I'll try again.
TC CorePlus v.11.1 i686 & lots of coffe

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 404
Re: Help with my bash script
« Reply #3 on: April 06, 2020, 05:23:45 PM »
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.


Offline ferran

  • Full Member
  • ***
  • Posts: 159
Re: Help with my bash script
« Reply #4 on: April 06, 2020, 06:54:58 PM »
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.
TC CorePlus v.11.1 i686 & lots of coffe

Offline ferran

  • Full Member
  • ***
  • Posts: 159
Re: Help with my bash script
« Reply #5 on: April 06, 2020, 07:50:08 PM »
Ey guys !

Finally I fixed a little syntax problem in the stat line and it's no longer wrong.

I already have the code below:

Code: [Select]
#!/bin/sh

#####
#
# Description: Bash script to edit any text file wih an GUI editor
#
# Comments: At final It will restore the original permissions
#
#      NOTE: you need loaded the extension coreutils.tcz
#
#      Execute with ./editfile.sh <file to edit>
#
# Name: editfile.sh
#
# Author: Ferran
#
#      Date: Apr 7 2020
#
#####

## Get the arguments from commands line ##

argFile="$1"

echo "the file selected is $argFile"

## Get the permissions of the file and save it

getPerm=$(stat -c '%a' $argFile)

echo "the chmod of $argFile before was $getPerm"

## Set temporal permissions to 777 to open the file

sudo chmod 777 $argFile

## Execute the editor to open the file

editor $argFile

echo "The edition is finished."

## When comeback restore the original permissions

chmod $getPerm $argFile

setPerm=$(stat -c '%a' $argFile)

echo "the chmod is $setPerm again"

## Exit of sudo mode

exit


Hurra!  ;D
TC CorePlus v.11.1 i686 & lots of coffe