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}
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:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied
(2 answers)
Closed 4 years ago.
I am getting this error --
Incorrect number of bindings supplied The current statement uses 4,
and there are 3 supplied.
Here is the code --
def update_entry(self):
self.cur.execute('UPDATE SINGLE SET IN=?,OUT=?,QUALITY=? WHERE ID=?',
(self.IN_entry.get(),self.OUT_entry.get(),self.QC_entry.get()))
You have 4 placeholders in your query and yet you're supplying only 3 items in the tuple passed to execute. You should supply a value for the placeholder for ID as the 4th item in the tuple.
This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Is it possible to build variable names from other variables in bash? [duplicate]
(7 answers)
Closed 5 years ago.
I'm trying to get get the VALUE of a 'nested' variable into another variable and/or use the value directly as shown below
Below is an example scenario which exactly explains where I'm stuck
$ USER1_DIR=./user1/stuff
$ USER2_DIR=./user2/stuff
$ USER3_DIR=./user3/stuff
#User will be taken as input, for now assuming user is USER1
$ USER="USER1"
$ DIR=${USER}_DIR
$ echo $DIR
>> USER1_DIR
$ DIR=${${USER}_DIR}
>> -bash: ${${USER}_DIR}: bad substitution
Challenge 1:
Get DIR value to ./user1/stuff when the input is USER1
or
Get ./user1/stuff as output when the input is USER1
After I'm able to get through Challenge 1, I've to add some content to a file in the user directory like below
Desired output is as below
$ echo "Some stuff of user1" >> $DIR/${DOC}$NO
# Lets say DOC="DOC1" and NO="-346"
# So the content has to be added to ./user1/stuff/DOC1-346
# Assume that all Directories exists
FYI, The above code will be a part of a function in a bash script and it will be executed only on a Linux server.
Note : I don't know what to call variable DIR hence used the term 'nested' variable. It would be great to know what is it called, greatly appreciate any insight. :)
You can use eval, variable indirection ${!...}, or reference variables declare -n.
In the following, I will use lowercase variable names, since uppercase variable names are special by convention. Especially overwriting $USER is bad, because that variable normally contains your user name (without explicitly setting it). For the following code fragments assume the following variables:
user1_dir=./user1/stuff
user=user1
Eval
eval "echo \${${user}_dir}"
# prints `./user1/stuff`
Eval is a bash built-in that executes its arguments as if they were entered in bash itself. Here, eval is called with the argument echo "${user1_dir}".
Using eval is considered bad practice, see this question.
Variable Indirection
When storing the name of variable var1 inside another variable var2, you can use the indirection ${!var2} to get the value of var1.
userdir="${user}_dir"
echo "${!userdir}"
# prints `./user1/stuff`
Reference Variables
Instead of using indirection every time, you also can declare a reference variable in bash:
declare -n myref="${user}_dir"
The reference can be used similar to variable indirection, but without having to write the !.
echo "$myref"
# prints `./user1/stuff`
Alternatives
Your script may become easier when using (associative) arrays. Arrays are variables that store multiple values. Single values can be accessed by using an index. Normal arrays use natural numbers as indices. Associative arrays use arbitrary strings as indices.
(Normal) Arrays
# Create an array with three entries
myarray=(./user1/stuff ./user2/stuff ./user3/stuff)
# Get the first entry
echo "${myarray[0]}"
# Get the *n*-th entry
n=2
echo "${myarray[$n]}"
Associative Arrays
Declare an associative array with three entries
# Create an associative array with three entries
declare -A myarray
myarray[user1]=./user1/stuff
myarray[user2]=./user2/stuff
myarray[user3]=./user3/stuff
# Get a fixed entry
echo "${myarray[user1]}"
# Get a variable entry
user=user1
echo "${myarray[$user]}"
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`
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.