Use numeric value of input argument in Bash script [duplicate] - linux

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

Related

Use subprocess in python to echo a variable [duplicate]

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.

Bash Scripting with function that takes an argument [duplicate]

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`

How can I know if a string contains only one or several words in Bash? [duplicate]

This question already has answers here:
A confusion about ${array[*]} versus ${array[#]} in the context of a bash completion
(2 answers)
Closed 6 years ago.
When I get the content of an array in a string, I have the 2 solutions bellow :
$ a=('one' 'two')
$ str1="${a[*]}" && str2="${a[#]}"
After, of course, I can reuse my string on the code
but how can I know if my variable has only one or several words?
In both cases, the contents of the array are concatenated to a single string and assigned to the variable. The only difference is what is used to join the elements. With ${a[*]}, the first character of IFS is used. With ${a[#]}, a single space is always used.
$ a=(one two)
$ IFS="-"
$ str1="${a[*]}"
$ str2="${a[#]}"
$ echo "$str1"
one-two
$ echo "$str2"
one two
When expanding $str1 or $str2 without quoting, the number of resulting words is entirely dependent on the current value of IFS, regardless of how the variables were originally defined. "$str1" and "$str2" each expand, of course, to a single word.
To add to #chepner's great answer: the difference between ${arr[*]} and ${arr[#]} is very similar to the difference between $* and $#. You may want to refer to this post which talks about $* and $#:
What's the difference between $# and $* in UNIX?
As a rule of thumb, it is always better to use "$#" and "${arr[#]}" than their unquoted or * counterparts.
"${a[*]}" expands to one string for all entries together and "${a[#]}" expands to one string per entry.
Assume we had a program printParameters, which prints for each parameter ($1, $2, and so on) the string my ... parameter is ....
>_ a=('one' 'two')
>_ printParameters "${a[*]}"
my 1. parameter is one two
>_ printParameters "${a[#]}"
my 1. parameter is one
my 2. parameter is two
If you would expand the array manually, you would write
${a[*]} as "one two" and
${a[#]} as "one" "two".
There also differences regarding IFS and so on (see other answers). Usually # is the better option, but * is way faster – use the latter one in cases where you have to deal with large arrays and don't need separate arguments.
By the way: The script printParameters can be written as
#! /bin/bash
argIndex=0
for argValue in "$#"; do
echo "my $((++i)). argument is $argValue"
done
It's very useful for learning more about expansion by try and error.

Linux environmental variable defined but not reflecting value [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 7 years ago.
Please help me understand and possibly explain this.
Suppose I have 2 env variables:
$REL --> foo
$MEL --> bar
Now if I echo $REL$MEL it works.
If I echo test$REL, it works.(testfoo)
But why Linux is not able to identify the variable when used like this: $RELtest?
I understand this looks like the stupidest question but is there a way to tell the system to look for the matching part of the string in env variables already set and replace that part?
I also understand that when doing test$REL, it is $ that works as an identifier.
If this is not possible then how to explain the limitation?
Any usage example in docs where this is shown prohibited will help me a lot.
With $RELtest, the shell interprets it as the value of the variable RELtest - it is not able to figure out that you meant REL followed by a literal test since there is no delimiter. You need to be more explicit - use {} to quote the variable:
$ echo ${REL}test
footest
See also When do we need curly braces in variables using Bash?

How Do I Find the Last Positional Parameter in Linux

I am looking to find the last positional parameter. I know to find the number of parameters is $# which logically is the last but I need to find and use what is in the last positional parameter.
It would be like if $# = 5, I want to do $5.
I have tried to do something like $$# or ${$#} as guesses but can't get it.
Instances where I would need to use it is in an if statement or declaring it to another variable or with echo.
I hope that is clear. Thanks in advance.
You want ${!#}, which combines the argument count with indirection.
This seems like a more direct approach:
${#: -1}
Some further information here. I'm not sure about portability outside of Bash.
Here is another way:
while [[ "$1" != "" ]];do
last_parm=$1
shift
done
echo $last_parm
Give a test:
$ ./test.sh 1 2 3 4 5
5

Resources