, , , ,

Difference between revisions of "Bash scripting"

From Camilo's web
Jump to: navigation, search
Mountain View
m
Line 54: Line 54:


Here the <nowiki>''$1''</nowiki> means that the script requires a parameter, which is this case is the file that you want to open.
Here the <nowiki>''$1''</nowiki> means that the script requires a parameter, which is this case is the file that you want to open.
=== Open a ''tex'' file with emacs and the corresponding ''pdf'' file with TexShop===
#!/bin/bash
open -a emacs "$1".tex & open -a TexShop "$1".pdf

Revision as of 16:47, 14 December 2021

How to create the script file

To create a bash script accessible globally throughout the terminal, you need to put it in a root folder accessible to the whole system. A good place to put it is

/usr/local/bin

There you can create (using your root role) the script file my_script, which should look like this:

#!/bin/bash
script line 1
script line 2
...

and once the coding is done, you need to make it executable

sudo chmod +x my_script

Examples

Reduce the size of multiple images

#!/bin/bash
mkdir Reduced

echo 'Write the name of the files'
read filename
echo 'Write the reduction percentage'
read percentage

j=0
for i in $( ls -p | grep -v / )
do
  let "j=$j+1"
done

j=1
for i in $( ls)
do
 cp $i ./Reduced/$j.jpg
 let "j=$j+1"
done

cd Reduced

j=1
for i in $( ls)
do
  convert $i -resize $percentage% $filename$j.jpg
  let "j=$j+1"
done


Alias to open a pdf file with TexShop

The script is called ts and it contains the following lines:

#!/bin/bash
open -a TexShop "$1"

Here the ''$1'' means that the script requires a parameter, which is this case is the file that you want to open.

Open a tex file with emacs and the corresponding pdf file with TexShop

#!/bin/bash
open -a emacs "$1".tex & open -a TexShop "$1".pdf