This question already has answers here:
How to pass all arguments passed to my Bash script to a function of mine? [duplicate]
(7 answers)
Closed 5 years ago.
I need to create a function and pass an argument like
myfunc word_100
and then the output should display
word_101
Basically it should increment taking in account the delimiter. I am thinking to say put word as one variable and the number and increment the number and combine it together. But not sure how to go about.
Try:
NAME=${1%_*}_
NUM=${1##*_}
echo $NAME`expr $NUM + 1`
Related
This question already has answers here:
Why does passing variables to subprocess.Popen not work despite passing a list of arguments?
(5 answers)
Closed 1 year ago.
I have a variable in python, and I'm trying to open a subprocess and echo the variable, then create a file with the variable in it.
I tried this:
subprocess.Popen(['echo "$var" > file.txt'], shell=True)
It creates the file, but it's empty. How can I get the result that I want?
In Python you don't use $ sign to use a variable. Also when you want to embed variable into string, you cannot just simply use variable name in string. You should do something like that:
subprocess.Popen(['echo "{}" > file.txt'.format(var)], shell=True)
This is great website which will explain you how to use .format method.
This question already has answers here:
How to use string.replace() in python 3.x
(9 answers)
Closed 5 years ago.
I am trying to write a program that replaces every 'a' for '&'.I am currently stumped and was wondering if someone could write it for me to get an idea of what it would look like. Thanks.
strw = "kabhi kabhi mery dil main khyaal aata hy"
print (strw.replace("a", "&"))
This question already has answers here:
Getting the last argument passed to a shell script
(29 answers)
Closed 7 years ago.
I'm wondering how to obtain the last argument passed to a bash function, like this:
#!/bin/bash
function hello() {
all=$# # all arguments
n=$# # number of arguments
first_arg=$1 # argument one
last_arg= ??? # How to get the last argument?
}
How to set $last_arg to the value of the last argument passed to the function?
If all holds the $#, then the last argument is ${all[*]: -1}
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 7 years ago.
I would like to use the numeric value of the input argument that I pass to a bash script like this:
./script_pb3.sh out.txt 3
Here I would like to take the second argument that I pass (i.e. the value 3) and use it as a numeric value inside my script, that looks like this:
#!/bin/bash
NUM=$2
for i in {1..$NUM}; do
echo $i
done
But when I run it all I get is
{1..3}
Instead of
1
2
3
Can you please provide an explanation for why this is happening and a workaround? I think that this question aims at casting a string value to an integer value but I am not sure. Any help is greatly appreciated.
EDIT: the answer from here contains a debate concerning the answer to my issue (i.e. it assumes the answer is already known), but it does not come as an answer to my question, therefore I didn't find it in my first searches. Thank you, though, for pointing it out.
I think you have to use seq
for i in `seq 1 10`;
do
echo $i
done
Reference: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
So, what you're looking for is : seq 1 $2
This question already has answers here:
Getting the last argument passed to a shell script
(29 answers)
Closed 7 years ago.
To access to the first argument of a function, I use
func() { echo $1; }
How to access directly to the last argument of a function in ash?
I do not want to use loops neither functions or complicated commands
You can use $($#).
$# is the number of arguments (which is equal to the index of the last argument), so $($#) is the last argument.