use commandline arguments in bash aliases [duplicate] - linux

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

Related

How to redirect to a root file as non-root with sudo [duplicate]

This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 1 year ago.
How to do that with a regular sudoers user:
wget -q https://example.com/binary.tar.gz -O - | tar -xzO binary > /usr/local/bin/binary # Doesn't work because I can't write in /usr/local/bin as regular user
I can't use sudo with the redirect >. So what is the appropriate way to do it?
Subsidiary question: I know I can use tee for the following case:
echo "foo" > /etc/myapp.conf # Doesn't work
echo "foo" | sudo tee -a /etc/myapp.conf # Solution
But I know this is not the goal of tee. So the question is the same for this case: what's the best solution?
EDIT:
I don't want to use a subshell with something like sudo sh -c 'my-command'. One reason is to limit as much as possible the number of commands launched as root.
This is a question to find a solution in the "linux standard", not some kind of hacking.
I think the goal of tee is to write output to some files AND in the stdout. I don't want to use something which can do that, but something which exists to do that.
The reason you cannot redirect to a file you don't have write access to, is because the shell does the opening of the file. When using sudo only the process that are ran with special privileges, consider the following example:
$ sudo my-cmd --flag1 --flag2 > file
The shell invokes the command sudo with the arguments: my-cmd, --flag1 and --flag2, and tries to open a file called file, and connect the sudo process' standard output to the input of the file.
The command sudo will elevate the users permissions and execute the command my-cmd with the arguments --flag1 and --flag2.
Understanding this it also becomes clear why you cannot just prefix with sudo to write to a file that you don't have permissions to.
Instead you can, as you have already discovered, use tee as it can take a path as an argument and open it for writing.
Alternative you can invoke another shell with special privileges:
$ sudo sh -c 'my-cmd --flag1 --flag2 > file'
This will invoke the shell sh with escalated privileges. This however becomes quite tedious as you'll need to stick your whole command into one argument for sh. This will cause quotes and glob to be hard to write properly.

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

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.

Using the hostname in a String [duplicate]

This question already has answers here:
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 3 years ago.
I'm trying to use the hostname of a linux device into a string for that here's what I'm trying:
app.sh
flowname="flow_${hostname}.txt"
echo "$flowname"
running the script delivers the following output : flow_.txt
any idea what I'm missing here ?
thanks in advance !
There's no built-in variable named $hostname. Try the variable $HOST or the command substitution $(hostname) instead.
flowname="flow_$HOST.txt"
flowname="flow_$(hostname).txt"
The ${VARIABLE} syntax is used to substitute the value of the given variable.
You want to execute the hostname command.
By using the above variable substitution you try to output a variable called 'hostname' which does not exist.
You have to use the $(COMMAND) syntax. This will execute the given command and print its result.
Try "uname -n", the "uname" command gives interesting information on your host.
I tried your script under ubuntu on WSL (windows subsytem for linux) and found the comment of John Kugelman useful.
To make the script functioning in this environment, i first print out the available system environment variables.
printenv | less
then I chosen to replace ${hostname} with ${NAME}.
You need variables that are populated. Use the command env to list environment variables,
env
So, just populate hostname,
hostname=$(uname -n)
echo ${hostname}
flowname="flow_${hostname}.txt"
echo "$flowname"

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)

How to know the default shell process for my terminal? [duplicate]

This question already has answers here:
How to get the default shell
(5 answers)
Closed 6 years ago.
I am composing a bash script to serve as a utility tools.
The challenges I am facing now is:
- user using my tool will be running in bash environment
- however, some of them might default using krcsh or tcsh. they might have aliases or configurations set in there.
So, I need to prompt/guide user to resolve this during installation. My first challenge: How am I suppose to know the user's default shell within my install.sh?
Knowing the "default" shell, I can prompt and guide the user to do necessary transfer to bash.
my testing code:
my result:
1/ is fault obviously. It return the current shell which is my install.sh (bash)
2/ I am doubtful. It seems to be the history of what I have run before. It does not show me my default configured shell. My case, my terminal default shell is bash, and I run tsch for testing purpose. So the script parsed wrong information and will though my default shell is tcsh. It will then assist me to port configurations from tcsh to bash during the installation process.
If you want to check the shell you are using, you can use the following methods:
echo $0 in terminal will show you the program running if you want to check the shell you are currently using.
echo $SHELL - with this command you can read the user's default shell in the terminal you are running.
If you want to prompt, easily you can put the echo $SHELL in the part of your script where you need to show the current shell you are using.
Don't forget to put #!/bin/bash if your script is designed to run in a bash shell!

Resources