Find the answer to your Linux question:
Results 1 to 7 of 7
Hello, i'm completely new to linux and shell programing i'm using linux fedora v.8, bash i want to know how to print argument value depending on other argumet value. ex. ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4

    Unhappy Shell Programming

    Hello,
    i'm completely new to linux and shell programing
    i'm using linux fedora v.8, bash
    i want to know how to print argument value depending on other argumet value.
    ex. i have two txt files
    the first one includes 2 arguments $1, $2 (these values r entered through program)
    the second one includes 2 arguments $3, $2 (these values r entered through program)
    now through shell program the user will enter $3 value, so i want through this value to print $1 value, any help?


    - then i want to know how to consider each word that entered by user as an argument and how to print the third word (as an ex.)
    ex. the user will enter this line;
    test test1 test2 test3 test4 ....
    so how to print the third word?

    i hope that my questions are clear

    thanks in advance

  2. #2
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    Apologies but I don't really understand your first question. If you are more specific about your example it might help.

    Second question:
    If I have a file test.sh containing:
    Code:
    #!/bin/bash
    echo $3
    And I run it with your arguments I get:
    Code:
    bash-3.2$ test.sh test1 test2 test3 test4
    test3
    Hope this helps

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4
    Hello,
    thanks a lot for your response, then
    about the first question,
    i have wo txt files
    1- includes information as follow (Student_ID, grade class )where student_id = argument, and grade class = one argument.
    2- includes student_name, student_ID

    now the user will enter the student_name to print argument value; grade class.

    i hope that it's much better now.

    then about the second q how could i deal with that if i don't know how many words the user will enter per line?

    thanks again

  4. #4
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    The question really depends on where the files are situated but I'll give you a start and you can work from there. If I have the following text files in a directory:
    Code:
    bash-3.2$ for a in `ls *.txt`; do echo "$a : `cat $a`";done
    id.txt : John,1235
    id1.txt : Patrick,1234
    result.txt : 1234,B+
    result1.txt : 1235,B-
    If I have the script, getGrade.sh, in the same directory containing:
    Code:
    #!/bin/bash
    
    name=$1
    
    id=`fgrep $1 * | cut -d , -f 2`
    gradeAndId=`fgrep $id * | cut -d , -f 2`
    grade=`echo $gradeAndId | sed "s/$id *//g"`
    echo $grade
    Running it I get:
    Code:
    bash-3.2$ ./getGrade.sh Patrick
    B+
    bash-3.2$ ./getGrade.sh John   
    B-
    The big problems around this is how you deal with security and people with the same name but I'll leave that up to you.

    For the follow up question I think you should look at $@ and $#. $@ will give you all the arguments entered and $# will give you the number of arguments entered

  5. #5
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4
    Hello birdman,
    thanks a lot for ur help,
    as for the first question i've tried the way you gave me but it didn't work,
    it printed student ID, and a list of files names that related to this program (id.txt, id1.txt ..... )

    so what to do?

    as for the second one every thing is ok now

  6. #6
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    I should probably give a greater explanation of what I did.

    I tried to guess what your file structure would be. This was the first part I showed where I printed out the contents of all the .txt files in the directory I was working in. If your files are structured differently the script won't work but an explanation is probably in order anyway.

    The script assumes that the user supplies the name as the first argument. It then looks for this name inside the files in the current directory (fgrep $1 *) and then uses the pipe symbol (|) to send the results to cut so as to get the id of the user. This part of the script assumes that the username is unique (If it is not unque you need to put some more checks in). The next part of the script just does something similar looking for the id and then cutting out the result.

    If you gave me an example of the structure of your files I could probably give you a better start and it would help if I knew the context in which the script will be used. For your own learning it would be good to go through every command to see what it does (e.g. use the man pages or step through everything in the script using echo to print out the steps)

  7. #7
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4
    Hello bird man,
    all my files are exist on the home directory.
    then the structure of my files is;
    the first app is asking you to enter Student ID and his Grade, and then its saving it in a txt file 1.txt
    then the second app is asking you to enter Student name and Student ID and saving it into nother txt file 2.txt

    now the third app which i'm working on it now, its asking you to enter Student name to print his grade.

    (then in the case of that i have unkown number of arguments in the first app, i mean argument for Student ID and a list of Grade argument, how could i deal with that? )

    as for the checking operations i've already solve it.

    i hope that every thing is clear now, and thanks again for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...