, , , ,

Difference between revisions of "Bash scripting"

From Camilo's web
Jump to: navigation, search
Mountain View
m
 
(One intermediate revision by the same user not shown)
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
=== Execute a ''.m'' script and dump its output into a log file===
#!/bin/bash
/Applications/Mathematica.app/Contents/MacOS/MathKernel -run "<<$1" > $2 &
Here the <nowiki>''$1''</nowiki> stands for the name of the script that you want to execute and <nowiki>''$2''</nowiki> indicates the log file to which you want to dump the outcomes of the execution

Latest revision as of 17:06, 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

Execute a .m script and dump its output into a log file

#!/bin/bash
/Applications/Mathematica.app/Contents/MacOS/MathKernel -run "<<$1" > $2 &

Here the ''$1'' stands for the name of the script that you want to execute and ''$2'' indicates the log file to which you want to dump the outcomes of the execution