Shell script to replace long predefined arguments [duplicate] - linux

This question already has answers here:
Propagate all arguments in a Bash shell script
(12 answers)
How to write a bash script that takes optional input arguments?
(9 answers)
Closed 2 years ago.
This is probably the worst question I've asked, I wasn't entirely sure how to formulate it and google results are best when the search is short and concise.
So, I keep finding myself constantly running a more or less identical list of commands in a Linux terminal. For example:
./some_script -argument1 -argument2 -argument3 [varying list of parameters that differ in type]
Now, the script and first 3 arguments are always the same. I was thinking if there is a way, such that a new script could be written, so that my entire input is much shorter. Like:
./new_script [varying list of parameters that differ in type]

This seems like a perfect place for an alias:
alias new_script='./some_script -argument1 -argument2 -argument3'

Create a file new_script in some directory in your PATH, for example in /usr/local/bin/. I usually create ~/bin directory and add it to PATH. Some people like to follow XDG specifications and add ~/.local/bin to PATH.
The file needs to have executable rights and following contents:
#!/bin/sh
./some_script -argument1 -argument2 -argument3 "$#"

Related

How to run bash file with one word [duplicate]

This question already has answers here:
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
Closed 1 year ago.
Issue
I want to run a bash file more easily, I've seen some applications where you only need to type word to execute the script.
Instead of typing ~/folder/file.sh in the terminal,
I only have to type a_word to run the file.
Is this possible with bash?
And also, this is on RPiOS's terminal, not sure if it differs.
Save your file to a location named in PATH. /usr/local/bin/a_word (no .sh) is a great example of such a location. Make sure it has executable permissions and starts with a shebang (like #!/usr/bin/env bash).
When you want to install something just for your own account, it's common practice to create a ~/bin directory and add it to your PATH (as by adding something like PATH=$PATH:$HOME/bin in ~/.bash_profile).
You have to define a so called alias.
Edit the file $HOME/.bashrc and add alias a_word = '$HOME/folder/file.sh', then logout and logon again.

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

Bash script output not showing up during execution? [duplicate]

This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have created a virtual environment on my debian system and i made a script that activates it (should).
However when i execute the script nothing shows up, not even an error, my guess is that it is running in a different shell or something but I don't know how to solve it.
Here is the code of the script
#!/bin/bash
source ~/PythonEnv/environments/my_env/bin/activate
I have changed the permissions already with chmod u+x, so that is not a problem.
When i execute the script nothing shows up at all. Any thoughts???
Add set -x at the beginning of your bash script will do the trick.
-x Print commands and their arguments as they are executed.
You can see more bash options here
http://linuxcommand.org/lc3_man_pages/seth.html
Adding x-permissions is not necessary, since you are using source with an absolute path. Of course this sets the environment only which is executed by the shell script which you have posted here. If you want the changes in your interactive shell, it is pointless to do it inside a script. You have to source the activate script in your shell (respectively inside that process where you want the environment to be modified).

Add lines to file in linux [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 3 years ago.
I want to add a line to a file maintaining the exact pattern
Line i want to add:
export PATH=$PATH:$JAVA_HOME/bin
I dont want to add the values of the variables to the file
What I did:
echo "export PATH=$PATH:$JAVA_PATH/bin" | sudo tee -a /home/admin/Vishal/test.sh
My Output:
Contains numerous paths instead of export PATH=$PATH:$JAVA_HOME/bin
The immediate problem is that you need single quotes instead of double. But really, you should not be editing your script file. Instead, make it accept a parameter which tells it whether or not to update the PATH.
case $1 in --update-path) PATH=$PATH:$JAVA_HOME/bin;; esac
If you run /home/admin/Vishal/test.sh --update-path it will add the Java directory; without the option, it won't.

How to call variable from properties file in shell scripting bash [duplicate]

This question already has answers here:
How do I grab an INI value within a shell script?
(32 answers)
Closed 6 years ago.
This is my file (xyz.properties)
abcd.123=localhost:8180
Now I need this IP address in my shell script
vi create.sh
#!/bin/bash
How do I call abcd.123 from properties file to this shell script
!bin/bash
source = /xyz.properties
${abcd_123}
${"abcd_123"}
${abcd.123}
nothing works
this way is not working and my main idea is to use the variable everywhere
BTW i cannot use abcd_123 in my properties file
as there are so many dependencies on that variable
You can replace the dots and source the modified content:
$ source <(sed 's#\(.*\)\.\(.*\)=#\1_\2=#' xyz.properties)
$ echo $abcd_123
localhost:8180
in your bash script you need to "source" your properties file (you can use the "source" or "." [a dot]):
#!/bin/bash
source yourfile.properties
Edited. Change your names to use an underscore instead, then access them like so:
${"abcd_123"}

Resources