Run PERL script in BASH - bash

I use 2 BASH scripts which convert text to file with .mlf extension.
Definition of output in 1. script:
outfile="${mid}_textgrid.mlf"
i.e: 1_textgrid.mlf
Script is runned by:
bash /var/scripts/textgrid-to-mlf-refference.sh $1
Definition of output in 2. script:
outfile="${mid}_vtt.mlf"
i.e: 1_vtt.mlf
Script is runned by:
bash /var/scripts/vtt-to-mlf-hypothesis.sh $1
mid(multimedia identifier) is defined in another script that creates these files. These files are used to compare using compare.pl script(written in PERL). I can run this script using terminal: i.e: ./compare.pl 1_textgrid.mlf 1_vtt.mlf
Problem is that I want to run this script automatically with BASH script. I tried it in script using: perl /var/scripts/compare.pl $1_textgrid.mlf $1_vtt.mlf But it didn't work. Can you give me an example how to run it correctly in this script?

I made new script in BASG to run this "comparing":
#!/bin/bash
mid=$1
reff="/home/var/www/vids/$mid/${mid}_textgrid.mlf"
hyp="/home/var/www/vids/$mid/${mid}_vtt.mlf"
out="/home/var/www/vids/$mid/${mid}_wer.txt"
perl /var/scripts/mlf_compare.pl $reff $hyp >> $out

Related

Running a cd command after user input in bash script will not change directory [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 6 years ago.
I am trying to create a script whereby I have list of numerical test folders and want users to be able to cd into one of them after inputting the folder number.
The script correctly concatenates the input but on running the script it does not actually execute the cd command to the required directory?
It echo's to the screen but then just sits there as if awaiting a further prompt?
Can anyone advise what I am missing please? Script 'chgdir' is as below:
#!/bin/bash
#
# Script to move to test##dir (using input from user for dir number)
echo "Enter test directory number as ## and hit Return"
read dirnum
echo "cd /home/John/test$dirnum""dir"
However on running the script outputs the command to the screen but does not 'cd' and just remains in ~/bin?
cd /home/John/test01dir
John#John-PC ~/bin
P.S I am completely new to bash scripting as you can tell so any help really appreciated.
All your script does is to echo the command that you formed. You need to actually execute the cd command as well as just echoing it.
cd /home/John/test ${dirnum}dir
The {} around the variable name allows the shell to distinguish the variable name from the extra characters appended after it.
That will change the directory inside the script. To have it apply afterwards, you will need to source the script (with dot "." or "source") to affect the shell you are running in.
Your script just prints the command. That's all the echo command does. It doesn't execute it, because you didn't tell it to.
You could execute the cd command by replacing the echo command with this:
cd "/home/John/test${dirnum}dir"
But if that's the last line of your script, it won't do anything useful. Doing a cd inside a script doesn't affect anything but the script itself.
If you want to cd from a script and have it take effect in the invoking shell, you can source the script rather than executing it:
. ./thescript
or you can have the script print the command you want to execute and eval its output:
eval "`./thescript`"
(To be clear, if you source the script using the . command, it needs to execute the cd command; if you val its output, the script needs to print the command.)

getting permission to execute a bash script

im trying to get my server to execute a simple bash script:
#!/bin/bash
echo Hello World
After saving this to /var/www/script (im saving it to the web directory for no reason in particular) i try and execute it with
exec /var/www/script
This fails returning i don't have permission to execute it, sudo exec isn't a thing so i do sudo -i then run exec /var/www/script as root and i still have permission denied. I fairly uncertain why executing it as root doesn't work. Im wondering if i'm
A) using the wrong command to execute a bash script
B) have incorrect formatting in the script
C) shouldn't have saved it to /var/www/
D) done some other thing that i'm not even aware of.
Im running ubuntu server 16.04 if that helps.
File Permissions
First, make sure that you have the correct file permissions:
chmod +x /var/www/script_name #Gives the current user execute permissions
Executing Your Bash Script
In order to execute your bash script, the easiest option is to just simply call it (without any additional commands) by typing in the relative path to the script:
/var/www/script_name
There are other options for explicitly executing your script from the shell (in your case, use the bash shell to execute your script as a bash script). From TLDP documentation...
A script can also explicitly be executed by a given shell, but generally we only do this if we want to obtain special behavior, such as checking if the script works with another shell or printing traces for debugging:
rbash script_name.sh # Execute using the restricted bash shell
sh script_name.sh # Execute using the sh shell
bash -x script_name.sh # Execute using the bash shell
A Note on File Extensions: "Shebang" line > File extension
It is not an advised practice to use file extensions with your scripts, especially if you think your code may evolve beyond its current functionality.
Just in case you were wondering if the file extension may be your problem... it is not. It is important that you know that the file extension of a script isn't necessary at all. What matter is what you put in the "shebang" line:
To use the sh shell:
#!/bin/sh
To use the bash shell:
#!/bin/bash
It won't matter what file extension you use - the "shebang" line indicates what shell will be used to execute the script. You could save a script with the "shebang" of #!/bin/bash as script_name.py, but it would remain a bash script. If you attempt to execute it, ./script_name.py, it would be executed as a bash script.
As #Arjan mentioned in the comments, using file extensions for your script could lead to unnecessary complications if you decide to change the implementation of your project (i.e., a different shell / language):
I could decide later to shift my project to sh, python, perl, C, etc. Perhaps because I want to add functionality. Perhaps because I want to make it portable to a system without bash. It would be much more difficult if I used the .sh file extension, since then I'd need to change all my references to the script just because I changed its implementation.
You have two choices:
Run it as an argument to bash:
bash /var/www/script
Alternatively, set the execute bit:
chmod +x /var/www/script
And, now you can execute it directly:
/var/www/script

Bash script called from octave

I'm using octave in order to create and execute a script. The script file is created successfully but it is executed correctly ONLY when I execute it from shell.
e.g. if I create the script file containing this line
for L in {1..5}; do > ${L}.txt; done
calling it from shell it creates 5 files
but calling it from octave ( using system("./myscript.sh"); or unix("./myscript.sh"); ) it creates only one file having name "{1..5}.txt"
My actual aim is not to create empty files, the above was just an example. In my script I'm using for loops which fail to be executed from octave.
Try "ps -f" to see which shell Your "myscript.sh" is executed under.
Try adding strict/enforced shell-specifier at the first line to tell it to run under bash.
E.g.
#!/bin/bash
ps -f
for L in {1..5}; do > ${L}.txt; done

Pass string to shell script when run as command

I'm trying to write a shell script that builds an iso image from an .asm file with several utilities. When you run the script from the command line it first asks the user where the file is and where the iso file should be. I want the script accept arguments the user passed as part of the command: ./mkiso foo.iso bar.asm? The two arguments would correspond to two variables, input and output. How do I do this? EDIT: I'm using Linux and my script is a bash script.
mkiso:
#!/bin/bash
input=$1
output=$2
echo "input: $input, output: $output"
Run it with parameters
./mkiso aa.iso bb.asm
Output
input: aa.iso, output: bb.asm

Running a shell script through AppleScript

This is my first time trying to create a terminal script and then using AppleScript to run the code. I've tested my terminal script line by line in the terminal (it works...), but would like to put it in a file that can be called by applescript. So far, I've created a file called "/Applications/MAMP/htdocs/global_admin/import_database_command_line.sh" where I've saved all of the individual commands, the first being:
/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot;
I then use AppleScript to call the script as:
do shell script
"/Applications/MAMP/htdocs/global_admin/import_database_command_line.sh"
Unfortunately, for each line in my script I get an error, such as:
error "/Applications/MAMP/htdocs/global_admin/import_database_command_line.sh:
line 1: : command not found
Any help in coordinating my AppleScript and the file that contains my shell commands would be appreciated.
You need to include #!/bin/sh in the top line of your .sh file
#!/bin/sh
echo "Hello, world!"
You then need to make the script executable:
chmod +x import_database_command_line.sh
This is because the 'do shell script' AppleScript command is expecting to run the shell commands contained within the quotes. It is not expecting to be calling another script as you are doing. Just put your commands within the quotes, separated by semi-colons.
do shell script "/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot; #other_command; #etc"
Link

Resources