accessing a positional parameter through a variable
Do
[[ "$var" -eq "1" ]] && echo "$1" #for positional parameter 1
and so on or you could do it like below :
#!/bin/sh
var=2
eval echo "\$${var}" # This will display the second positional parameter an so on
Edit
how do I use this if I actually need to access outside of echo. for
example "if [ -f \$${var} ]"
The method you've pointed out it the best way:
var=2
eval temp="\$${var}"
if [ -f "$temp" ] # Do double quote
then
#do something
fi
Access positional parameters from within a function in Bash
Just for the record, this is possible in extdebug
mode through BASH_ARGC
and BASH_ARGV
variables, but populating a global array with positional parameters is much easier than that, and has no side-effects.
$ bash -O extdebug -s foo bar
$ f() {
> declare -p BASH_ARGC BASH_ARGV
> echo ${BASH_ARGV[BASH_ARGC[0] + BASH_ARGC[1] - 1]}
> }
$ f 42 69
declare -a BASH_ARGC=([0]="2" [1]="2")
declare -a BASH_ARGV=([0]="69" [1]="42" [2]="bar" [3]="foo")
foo
assign last positional parameter to variable and remove it from $@
Use a loop to move all arguments except the last to the end of positional parameters list; so that the last becomes the first, could be referred to by $1
, and could be removed from the list using shift
.
#!/bin/sh -
count=0
until test $((count+=1)) -ge $#
do
set -- "$@" "$1"
shift
done
LAST=${1-}
shift $((!!$#))
echo "$LAST"
echo "$@"
Positional parameter not readable inside the variable - bash script
If you invoke your script as ./mycript.sh $michael
and the variable michael
is not set in the shell, they you are calling your script with no arguments. Perhaps you meant ./myscript.h michael
to pass the literal string michael
as the first argument. A good way to protect against this sort of error in your script is to write:
#!/bin/bash
var1=$( linux command to list ldap users | grep "user: ${1:?}")
echo "$var1"
The ${1:?}
will expand to $1 if that parameter is non-empty. If it is empty, you'll get an error message.
If you'd like the script to terminate if no values are found by grep
, you might want:
var1=$( linux command to list ldap users | grep "user: ${1:?}") || exit
But it's probably easier/better to actually validate the arguments and print an error message. (Personally, I find the error message from ${:?}
constructs to bit less than ideal.) Something like:
#!/bin/bash
if test $# -lt 1; then echo 'Missing arguments' >&2; exit 1; fi
Related Topics
What Does Two Dots Before a Slash Mean? (../)
How to Timeout a Group of Commands in Bash
Looping Through Lines in a File in Bash, Without Using Stdin
What's the Max File Mapping Size in 64Bits MAChine
Changing Pecl Installation Directory
What Really Is the "Linger Time" That Can Be Set with So_Linger on Sockets
Linux Assembler Error "Impossible Constraint in 'Asm'"
Suppress or Prevent Duplicate Inotifywait Events
Capturing Display/Monitor Images, Sending Keyboard Input on Linux
.Zshrc Config File Syntax Error
Iptables Script to Block All Internet Access Except for Desired Applications
Simulating a Spinner for Progress in Bash