[Solved] I need to parse an username in parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] I need to parse an username in parameters
# 1  
Old 01-23-2012
[Solved] I need to parse an username in parameters

Hi!

Thank you ina advance for your Help. Sorry my english, Im from Argentina!
Great Place!!!

So, here si my problem

I have a variable, lets say PARAMETERS that could be set like this:
Code:
PARAMETROS="-Usistemas string1 -Astring2 -G -Y -Kcoco -Y string4 -Z"

or

PARAMETROS="string1 -Astring2 -G -Y -Usistemas -Y string3 string4 -Z"

# or

PARAMETROS="string1 -Astring2 -G -Y -Kcoco -Y string4 -Z -Usistemas"

So, I need to extract the name that is next to the -U flag (is the username)
In this case is sistemas

So the -U could came like a first parameter, in the middle or like the final parameter.

ALSO, it could be some cases where a U capital letter exist in the parameter variable, like this

#PARAMETROS="-Usistemas string1 -Astring2 -G -Y -KCUCU -Y string4 -Z"

So the only thing that is common to all exaples is the -U
Im not familiar with sed or awk, and its becoming difficult with cut and if.

Can you help me?

---------- Post updated at 10:26 AM ---------- Previous update was at 10:13 AM ----------

I figured it out!
Code:
UsrFromParametros () {
for i in ${PARAMETROS} ; do
    if [ e`echo ${i}|cut -c1-2` == "e-U" ] ; then
        USUARIO=`echo ${i}|cut -dU -f2`
        echo ${USUARIO}
    fi
done
}

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 01-23-2012 at 12:22 PM..
# 2  
Old 01-23-2012
Here's a way using string operations on the arguments (ksh93 on Solaris):
Code:
#!/usr/dt/bin/dtksh
for i in $*
do
  # If the first two characters = "-U" then the rest is the username.
  if [[ ${i:0:2} == "-U" ]]; then
    user=${i:2:${#i}}
  fi
done
print "USER: $user"
exit 0

Output:
Code:
$ ./argtest -Usistemas string1 -Astring2 -G -Y -Kcoco -Y string4 -Z
USER: sistemas
$

Cheat sheet on string operations:
Reference Cards

You may also want to read up on the getopts command for parsing command line options.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Why does "ps -[u|U] username" not list processes when username is numeric?

Greetings, The title pretty much says it all. I've snooped everywhere and can't find anything on this. Since our organization went to numeric usernames, using the u|U option for ps returns no processes. Example passwd entry: 320074:DjZAJKXun8HBs:10129:6006:Joe Y:/cadhome/analysis/jy:/bin/bash... (4 Replies)
Discussion started by: crimso
4 Replies

2. Shell Programming and Scripting

[solved] Awk/shell question to parse hour minute from text

Hi, I have a quick question on parsing the hour/minute and value from a text file and remove the seconds portion. For example in the below text file: 20:26:01 95.83 20:27:01 96.06 20:28:01 95.99 20:29:01 7.11 20:30:01 5.16 20:31:01 8.27 20:32:02 9.79 20:33:01 11.27 20:34:01 7.83... (2 Replies)
Discussion started by: satishrao
2 Replies

3. Shell Programming and Scripting

[Solved] Array for parameters from 5th position

Hi All, I am writing a script where the first 5 parameters are mandatory but the script should be able to handle a maximum of 9 parameters (with the remainig 4 optional) I would like to load all parameters from 5th parameter positioninto an array. the piece of code I am writing for this:... (0 Replies)
Discussion started by: snailrider
0 Replies

4. UNIX for Dummies Questions & Answers

[SOLVED] re-parse duplicate horizontally

I have a 2 column file. Column 1 is a road ID number. Column 2 is the date that the road surface condition was inspected. Some roads have been inspected only once, while some roads have been inspected multiple times. I would like to re-arrange the following example list: 1001 2001_01_01... (5 Replies)
Discussion started by: kenneth.mcbride
5 Replies

5. Shell Programming and Scripting

[Solved] Get username, etc. from config file

how can i get the database name from a config file its like this: // ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'userabc_wrdp1'); (there might be spaces in the front of "define") i want to store... (0 Replies)
Discussion started by: vanessafan99
0 Replies

6. Emergency UNIX and Linux Support

[Solved] AWK to parse adjacent matching lines

Hi, I have an input file like F : 0.1 : 0.002 P : 0.3 : 0.004 P : 0.5 : 0.008 P : 0.1 : 0.005 L : 0.05 : 0.02 P: 0.1 : 0.006 P : 0.01 : 0.08 F : 0.02 : 0.08 Expected output: (2 Replies)
Discussion started by: vasanth.vadalur
2 Replies

7. Programming

Parse parameters with getopts

Hi, this is my problem I have script with two parameters -n name and -s surname. Both have arguments and I want to know how parse these parameters with getopts. When you write ./names -n John -s White it find you all persons, which name is John White, but when you write ./names -n John ... (1 Reply)
Discussion started by: frees
1 Replies

8. Shell Programming and Scripting

Parse HTML tag parameters and text

Hi! I have a bunch of HTML files, which I want to parse to CSV files. Every page has a table in it, and I need to parse each row into a csv record. With awk and sed, I managed to put every table row in separate lines. So my file looks like this: <TR> .... </TR> <TR> .... </TR> ...One... (1 Reply)
Discussion started by: senszey
1 Replies

9. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

10. Shell Programming and Scripting

parse long input parameters

anybody know a nice way to parse long input parameters such as --path /dir1/dir2/ (see below). Now I have more than 10 input parameters and it's confusing having parameters like -q something, I would prefer longer ones case $OPTKEY in --path) M_PATH=$OPTARG ;; -s) ... (3 Replies)
Discussion started by: larne
3 Replies
Login or Register to Ask a Question