Pass string to shell script when run as command - bash

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

Related

How do I write a shell script that takes a data file name as an argument to run my perl script?

I am learning Perl and Shell scripting and of the challenges I was given is to write a shell script that takes a csv file as an argument and then have that shell script run my perl script (test.pl). I can't seem to get the proper syntax down and I have terminate every time because it hangs up my terminal.
For example shell script is test.sh
#/bin/bash
./test.pl $i`
on my terminal I write type out
test.sh testfile.csv
Ultimately I want the test file to be read by my perl script so it can run.
I think your error comes from the $i` part.
First the trailing backquote is probably a typo and should raise a syntax error. Second, the i variable is not defined, so $i resolve to an empty string. As it is not quoted, shell will omit it and call test.pl without any arguments... Thus your terminal is probably hanging because your perl script is waiting for input.
As #fra suggested, you should use $1 instead of $i, hence passing the first argument passed to your bash script, to your perl script.
Depending on your perl script (shebang, execution write) you may or may not call the perl executable manually.

append text to file using shell from php

I trying to append passed text as parameter to file using shell script
this is the code of shell
echo $1>>/etc/freeradius/mods-enabled/ldap
this shell will get the text to add it to the file ldap in /etc/freeradius/mods-enabled path , I call this shell in this format
# sh /etc/append.sh hello how are you man
but in this example the shell only get first word 'hello' and append it to file .how can I tell shell that all words are same variable and should insert to file
It depends on the shell you're using.
In bash the this script should work:
#!/bin/bash
echo "${#:1}">>/etc/freeradius/mods-enabled/ldap
Edit: As DTSCode pointed out in comments the :1 part in the script is redundant so this would be more correct:
#!/bin/bash
echo "$#" >> /etc/freeradius/mods-enabled/ldap
Then give the permissions to execute the file and call
/etc/append.sh hello how are you man
Or without execute permission call
bash /etc/append.sh hello how are you man

Run PERL script in 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

How to start a CLI program and give input to it using a bash script?

I have a third-party CLI program I downloaded using Node's package manager. This program is started by typing the name of the program in a terminal. Once you start the program, the program expects the user to enter strings of characters in which the program will interpret at proper commands if the strings of characters are those it recognizes. I want to automate the process of reading lines from a file, and passing these lines are strings of characters into the program.
Right now when I look up for help on Google for how to automate a CLI program, all I get it how to write a bash script. This is not enough, as what I need is a bash script that opens up a program and then passes arguments to that program, NOT to the terminal itself. Basically I need my script (which will take the file to read lines from as the only argument) to do the following
run my_program
while there are more lines to read from the file:
"Lookup"
$line
close my_program
where "Lookup" is a string of characters recognized as a command by my_program, and $line is meant to convey that I want to pass the line currently being read from the file as an argument to the program.
EDIT: I wrote the following script, but it's interpretting "while read line" as an argument to pass to my_program. How do I make it so that it only interprets the commands inside the while loop as arguments to my_program?
#!/bin/bash
while read line
do
my_program
"Lookup"
"$line"
done < $1
#!/bin/bash
while read line
do
my_program $line
done < $1
and run this script with input file as parameter

Effects of comment (#) lines before and/or after the comment-like #!/bin/sh line

Example one
#!/bin/sh
# purpose: print out current directory name and contents
pwd
ls
Example two
# purpose: print out current directory name and contents
#!/bin/sh
pwd
ls
What is the difference – if I make the first line a comment(#), with #!/bin/sh as the second line, what will happen?
What is meaning of #!/bin/sh ?
Normally a shell script is run by your default shell defined in the /etc/passwd file. But you can define explicitly a program which can run your script.
Unices uses a common method to determine what program needed to run a specific script (man execve(2)). If the script has the proper execute rights set and in a script the first line starts with a #! characters, it will run by the program defined afterwards.
For example if the first line is #!/usr/bin/awk -f then the rest of the file will be passed to the awk program (so it has to use awk syntax). Or if a Makefile starts with #!/usr/bin/make -f then the rest of the file will be passed to make. You can start the script as a normal program and the script can be written in awk or make (or whatever defined) syntax.
If execve does not find #! as the first two character of the file, it will consider as a normal script file and it will run as it is.
So using #! You can determine the script language and You do not need to know what shell is used by the other user using your script. In any other line #! will be interpretered your default shell, which is usually just a comment line.
what is difference between 1st & 2nd shell scripts..?
No difference in output. But the time to execute both will be little different as the interpreter reads line one by one.
if i give comment(#) in 1st line after #!/bin/sh in 2nd line so what will happen ?
Any line started with (#) except the shebang(#!) is treated as a comment in shell script.
what is meaning of #!/bin/sh ?
Its the path(here - /bin/sh) to the interpreter used after the shebang (#!) . Shell will try to use the interpreter language mentioned after the shebang to execute the script.

Resources