Register
It is currently Thu Oct 30, 2014 11:35 pm

trying to get user input var $1 using current $cntr value


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Aug 03, 2013 3:42 pm   

Joined: Mon Jul 01, 2013 7:24 pm
Posts: 19
I am trying to write a script so that a user must enter at least one input "$1", but could enter as many as need be
then the script needs to parse each user input using cntr=1 to start things off where "$cntr" = "$1" but I cannot seem to combine the user input var num "$1" with current "$cntr" var. Here is what I am trying:

Code:
cntr=1
while [[ "$cntr" -lt 9 ]] ; do   #9 is just an arbitrarily low number to use until the script works
   if [[ -z \$"$cntr" ]] ; then   #'if' user input "${$cntr}" contains no data then exit
                sort someFile > someFileSorted
                mv someFileSorted someFile
                more someFile
      exit 69
   else
      uInput="\$$cntr"   #user input "${$cntr}"
      #sed -ri "1i $uInput" someFile   #insert user input string into someFile
      echo "sed -ri 1i $uInput $someFile"
   fi
   ((cntr++))
done


Output:

The problem I guess is trying to assign current user input var, "$1" for example, by way of the current value for "$cntr", which shoudl contain the same number value as current user input var.

But output I get from running the script with just a single user input value (but it is the same no matter how many user input values I enter) is:
Code:
$ sed -ri 1i $1 someFile
$ sed -ri 1i $2 someFile
$ sed -ri 1i $3 someFile
$ sed -ri 1i $4 someFile
$ sed -ri 1i $5 someFile
$ sed -ri 1i $6 someFile
$ sed -ri 1i $7 someFile
$ sed -ri 1i $8 someFile


when of course I would like to see something more like: '$ sed -ri 1i test someFile' when I enter the word 'test' as my user input.

Any help on this one would sure be appreciated. I think the suggestion of arrays might come up, and although I have looked at trying to use arrays in bash in the past it has never been with success, so I would much rather prefer a solution more along the lines of what I am trying to achieve above, if possible.


Top
 Profile  
 PostPosted: Sun Aug 04, 2013 2:55 am   

Joined: Mon Jul 01, 2013 7:24 pm
Posts: 19
I hate crying wolf, but I was just looking at the pinned posts at the top of this forum and found exactly what I was looking (nice surprise) in the 'Positional Parameters (arguments)' post.

My script is now working as desired:
Code:
lpCnt=$#      #set total number of paramaters included with command in var "$lpCnt" for loop count purposes
((lpCnt--))      #remove the first command parameter, which is in fact the command to call the shell script, and set the second parameter listed immediately after the command ("$1") as the first parameter, 0

for ((i=0; i <= "$lpCnt"; i++)) ; do
   sed -ri "1i $1" $someFile
   shift 1
done

sed -ri 's/^[ \t]{0,}(.*)[ \t]{0,}$/\1/' "$someFile"   #remove any leading/training whitespace
sort -u "$someFile" > "$someFileTmp"   #sort file removing any duplicate entries
mv "$someFileTmp" "$someFile"   #rm tmp sort file
more "$someFile"

Kewlies.


Top
 Profile  
 PostPosted: Sun Aug 04, 2013 6:39 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

Code:
#!/bin/bash

nbParam2escape=2 #because $@ is an array (which index starts at 0), and 0 is script's name and path.
for i in "${@:$nbParam2escape}"; do echo "$i"; done
Code:
$ ./tinyScript "a b" c d
c
d
EDIT: err, this is not exactly what you're asking for, is it?

to iterate on each positional parameter
Code:
for i do echo "$i"; done #''in "$@"`` can be implicit
or to get the positional parameter's number:
Code:
$ someFunc() { for ((i=1; i<=$#; i++)); do echo "$i ${!i}"; done;}
$ someFunc "a b" c d
1 a b
2 c
3 d


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP