How to save bash command with a global variable [duplicate] - linux

This question already has answers here:
How do you run bash script as a command?
(4 answers)
Closed 2 years ago.
Is there any way to save something like
run-script = some_repetitive_command_you_use
And be able to execute it like
run-script possible_args

Use a function, for example:
doit(){
ls
echo "DONE IT $1"
}
doit once
or look at alias.
Possibly you could use
LS=/usr/bin/ls
$LS /tmp
Depends on what you want.

You can use alias command for that, check this post for more information and examples about this topic.

Related

How to execute text from stdout in bash [duplicate]

This question already has answers here:
Bash script - variable content as a command to run
(7 answers)
Closed 1 year ago.
So I have a command that's like
cmd1|cmd2|...|cmdN|execute
The output of cmdN is one line that I want execute to execute as if I copy pasted the output of cmdN into the terminal myself. I've tried to replace execute with bash, with $, and I tried to use xargs. (I'm still kinda confused on each of the options I've tried).
What's the simple answer here that's gonna make me wanna delete this post?
If you need to execute it in the current shell, use the eval command:
eval "$(cmd1|cmd2|...|cmdN)"
If it can be executed in a subshell, pipe to bash:
cmd1|cmd2|...|cmdN | bash

How to call a function when a sh file is called using terminal [duplicate]

This question already has answers here:
How can I run a function from a script in command line?
(10 answers)
Closed 1 year ago.
I have a code in a file named main.sh:
#!/bin/bash
function hello() {
echo $1
}
I want to call it from command line like this:
main hello teddy
and it should output like hello teddy.
Is that possible?. If yes, Please tell me.
Thank you in advance
It is possible depending on where you define the function. There are several bash startup files which are run on startup. For example, .bashrc:
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
So if you define your function in your .bashrc you will be able to run it just as if you had explicitly defined it using the shell.

Unable to print the public ip in shell script [duplicate]

This question already has answers here:
What is the difference between $(command) and `command` in shell programming?
(6 answers)
Closed 4 years ago.
Hi I am trying to print the public ip of the machine in a file using shell script. I am using the command
ip=${curl ipinfo.io/ip}
in my script file and it gives an error saying bad substitution. whereas this command works when i run it in command line.
Is this the right way to get the ip through the script?
Thanks in advance!!
You are capturing the result of program so you should use $(). The following should work for you (with the -s parameter to curl stopping unnecessary output)
ip=$(curl -s ipinfo.io/ip)

use commandline arguments in bash aliases [duplicate]

This question already has answers here:
How to pass command line arguments to a shell alias? [duplicate]
(11 answers)
Closed 5 years ago.
I am new to linux and trying to create an alias that starts mongodb service.
The original command is sudo service mongod start. I want to generalise this usage for any service.
i.e. something like alias startservice="echo <password> | sudo -S $1 start".
So I would call it like startservice mongod should run the first command. I came to know that I can use functions for the same. However, I don't have a clue on how to do this either way.
Because I want my function which I create to be able to be accessible across terminals. I am not sure on how to create functions that act in this manner. Please help me on this.
I have gone through these two links:
parameter subsitution in bash aliases
Alias with Argument in Bash - Mac
Your help would be appreciated.
Example of function in bash:
startService(){
echo "your-password" | sudo -S service "$1" start
}
startService mongod # test

Use return value of a shell command as a value in shell script? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Capturing multiple line output to a bash variable
For example I want to run ls command and take the return list as a value kept in a array in shell script.
Something like
run
#ls
fileA
fileB
fileC
kept this return list in a variable that keeps a array
variable A = ["fileA","fileB","fileC"];
I cannot give the exact notation for code since I do not know how to write shell script. After I learn this, I 'll.
#!/bin/bash
variableA=$(ls)
echo $variableA
That should be your shell script assuming that you have bash
Then all you'd need to do is chmod +x shell_script to make it executable.
If you use ls > contents.file the result of ls is saved to a file called contents.file.
Remember, > rewrites the entire file while >> appends to the last line.
variableA=$(ls)
echo "$variableA"

Resources