I am trying to add custom variable in a bash script alias command and not able to do it
I added the following in .bash_profile file
alias mvdb='mv ~/dbs/aw ~/dbs/aw-$1'
In the command line I am trying to run a script
mvdb "2017OCT20"
I want the folder named aw to be renamed as aw-2017OCT20 when I run command
This is a job for a function.
mvdb() {
mv ~/dbs/aw ~/dbs/aw-"$1"
}
Related
Following is the shell function which I execute remotely :
softrun() {
source /usr/local/bin/myscript.sh
#/bin/bash /usr/local/bin/myscript.sh
}
Command to execute above function stored in 'script1.sh' remotely is :
ssh user#host script1.sh softrun
But it gives error : No Such File or Directory
I have tried by 'source' & 'bash' method.
When I check manually the shell script exist in the location and is not a symbolic link
This isn't how you call a function in bash. You can't just pass a function name into the script. You need this in your script1.sh file:
#!/usr/bin/env bash
softrun() {
source /usr/local/bin/myscript.sh
}
$1
Notice the $1 at the end of the file. This is your first argument to the script, which you're passing over ssh, which happens to be your function name. The reason you're getting "No Such File or Directory" error is because in your example ssh thinks that softrun is yet another file it's supposed to run after running script1.sh
UPDATE. Forgot. I think you'll also need to wrap the script name + its argument into quotes, so ssh thinks it's one single argument and not two files to be executed.
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
I have a bash script that looks like the below. When I run it in the terminal, it just leaves a blank space. I want to be able to CD into this different location to get to the file I need OR ALTERNATIVELY is there a way of getting a file from a different location?
#!/bin/bash
# My first script
alias location="cd C:/Users/A591024/AppData/Local/Temp/TD_80/hq**/1212*1212/R*****"
do I maybe need to say something like "run location" underneath?
the final goal is to be able to get to a file inside the R****** folder and open up that file inside the window and try and grep from that..
also this is being done inside windows command line not linux
If you run this script, then Linux will create a new shell for you. The new shell will execute the alias command. Since there are no more commands after that, the new shell will terminate.
Since the alias command is executed in a new shell (or subshell), your current shell isn't modified. Hence, after the script ends, you won't notice any difference.
To make the current shell execute the command, use source or .:
source location.sh
. location.sh
Note that this . is a command and shouldn't be confused with the . folder which is an alias for the current folder.
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
If I run a custom bash function under shell console:
~/w/dotfiles/ruby [g:master-] ΒΆ repository_root
/Users/tian/Documents/workspace/dotfiles
And if I run the custom bash function under IRB:
irb(main):001:0> `repository_root`
(irb):1: command not found: repository_root
=> ""
How could I get the same result in IRB?
# declare
repository_root () {
if git_is_repository ; then
git_show_repository_root
fi
}
Assuming your function is in one of your bash profile files, you want to start up an interactive (-i) login (-l) shell to execute (-c) your function:
output = %x{bash -lic 'repository_root'}
One way to do this is to make a command out of the function.
Here's a short how-to
Create a shell script file that calls the function.
Create a .bin directory in your HOME and add it to $PATH in .bash_rc.
Place the shell script file in .bin.
source .bash_rc to update the $PATH variable you just changed.
Assuming you named the file fnx, just use the back tick operator or exec to run
the command - exec("fnx")
where is repository_root declared?
.bash_profile? .bashrc?
try to source that file before using repository_root
`. /path/to/file/declaring/repository_root; repository_root`