Hello, my problem is simple & I searched a lot but I couldn't find anything about it:
Basically I'd like to pass $i to a variable, $i being the positional variable; but it is unknown in the beginning so I can't do it like eg. myvar=$3, it HAS to be the "i"..
First, I tried myvar=$($i) (because $i is the number of i (ex 5), and not the positional variable ($5) so we would need in theory another $ to get to the positional variable), doesn't work
I tried this also, though I'm not sure if it's correct:
(in my code, i'd like myvar to get the (2+$a) number for the positional variable).
It's best to avoid eval if at all possible. If you don't consider its implications very carefully, it can be used to inject arbitrary code inside your program.
You can't directly substitute into a variable name like that, no, but if you're willing to do it in two steps, this will work in BASH or new KSH:
These 2 Users Gave Thanks to Corona688 For This Post:
Thanks for the replies; I'll try the ideas tonight..
With arrays it's doable (I thought of that too), but it complicates the code since I only need to do it for 2 parameters. I didn't know about eval, but in any case Coronas' seems to be the most simple, so is this code correct like this?
For anyone reading Corona's code and not knowing bash's
:
${!N} uses the ! pattern matching operator that expands to any variable with the
same name as the value of the variable, in this case N is 2. So, it finds all environment variables with the name "2", or $2.
These 2 Users Gave Thanks to jim mcnamara For This Post:
Thanks for the replies; I'll try the ideas tonight..
With arrays it's doable (I thought of that too), but it complicates the code since I only need to do it for 2 parameters. I didn't know about eval, but in any case Coronas' seems to be the most simple, so is this code correct like this?
..or do I need a $ for y? like :
Neither's correct, actually. The example as I gave it is the version you need to use.
Hi,
Is there a special positional variables for when using the dot (.)?
Scripts are as below:
$: head -100 x.ksh /tmp/y.ksh
==> x.ksh <==
#!/bin/ksh
#
. /tmp/y.ksh 1234 abcd
echo "yvar1 = $yvar1"
echo "yvar2 = $yvar2"
==> /tmp/y.ksh <==
#!/bin/ksh (2 Replies)
From the command line:
dions-air:scripts dion$ ls -l /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX
-rw-r--r-- 1 dion staff 157934 7 Jun 06:55 /Users/dion/Library/Application Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCXworks... (2 Replies)
Hi,
I have a script which will be executed using the below command,
bin/nutch crawl urls -dir /data/test/
bin/nutch - Script file
crawl, urls, /data/test/ - Parameters
-dir - Option
The above script should executed from a shell script named test.sh. I have the below code to execute... (2 Replies)
Hi,
I have to use the vaious kind of filters based on various fields in the input file like - count occurence of cases where "TRK-GRP" = 169 or like "ADDR-DIG" = 80080.
I don;t know the positional variable for all below fields. Please help.
Input File :
+++ BEST 12-05-27 15:06:49 MDI 3478... (2 Replies)
In a Bash script I used getopts command to let a user does something regards to the selected options. The question is: How do you find out what is the name of the file that user inserted in the command line like the following:
The good part is this file is always the last argument in the... (2 Replies)
I have a following problem:
#!/bin/bash
NUM=`cat accounts | wc -l`;
for i in {1..$NUM}
do
account=`awk "NR==$i" accounts`;
echo -e "\nAccount: $account\n";
sudo ./backup_maildir $account;
done "accounts" is a file with regular e-mail addresses, one in each line.... (2 Replies)
I am using variable to give the location of the file I am using but I get error.
Here is the code:
LogFile=/tmp/log.email
echo -e "could not close the service - error number $error \n" > $LogFile
well this is not all the code but is enough because the problem start when I try to use the... (3 Replies)
Hi,
I have a script that processes the positional parameters provided on the command line, or - if none are provided - uses some defaults instead.
I've currently got it written as follows, which works like a charm, but I was wondering if there is a different/other/better/... way of doing... (2 Replies)
I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk.
Story: I'm automating the (re)configuration of network interfaces,... (3 Replies)