Find the answer to your Linux question:
Results 1 to 7 of 7
I am currently working with shell scripting in Linux and this is what.. I wanna to achieve.. I have a file named "a" having some parameters in it. for ex: ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    11

    wanna to save the input to a file

    I am currently working with shell scripting in Linux and this is what.. I wanna to achieve..

    I have a file named "a" having some parameters in it. for ex: color= red and boys = 10.
    Now, I wanna to run a script, so that It will ask the values of color and boys from me to input,.
    I will then enter color-green and boys=12.

    Then it will update those values into the file "a".

    So that, when I will open the file :"a".. I will find the update values of color and boys as green and 12 respectively.

    As per my understanding, we need to locate the address of variable color and boys that were stored the values as red and 10 respectively earlier.
    Then we need to replace those values as green and 12 to that memory location.

    Kindly suggest me, if this will work or you have different opinion.

    Thanks,

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You are currently working with shell scripting.

    If you are not yet too familiar with how bash works, you might want to try googling for this:
    Code:
    bash tutorial
    and get more familiar with bash. I am sure there are others on this site who will jump in with their favorite bash tutorials and other resources.

    If, on the other hand, you're already familiar with bash, why not just give it a go and write a bash script to do what you want, and come to us with specific questions?

    You may wish to also explore other utilities such as sed, awk, and possibly perl. These will help you do the dirty work of finding a string on a given line and replacing it.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Posts
    11
    I think.. my question is quite spwcific.. I just elongated it so that anyone can easily understand and suggest me.. I am aware about bash ..

    Furthermore.. I need to achieve the above to solve a stack query.. which is a part of real question..

    And.. I don't know perl .. and tried googling also..
    Request you.. if you can better suggest me with the exact logic atleast .. rather than going for further studies.. rest scripting.. I will manage.. just a logic.. how to achieve that.,, replacing the values of variables in a file from the input..

    Thanks

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So let me understand this:

    We have a script. We will call this script "execute". execute contains a number of parameters. It might look like the following:
    Code:
    #!/bin/bash
    
    subject=boy
    color=red
    
    echo "The $subject is $color"
    Where "subject" and "color" are our parameters.

    We have then a second script. We will call this script "update". update's job is to update the assignments in any given Bash script (is this correct? If it just has to update execute, then we eliminate one step of the later algorithm).

    So the actual work here is update. Our algorithm has 3 parts:
    1) Read the given script to find all variable assignments. These are what we will replace. This step can be skipped if we only want the ability to update a single script (we will hardcode those values in).
    2) Have the user input new values for these variables.
    3) Rewrite the original script, using the new values.

    The first thing we need to decide is the structure of a variable assignment. This can be anything you want. For simplicity, you might consider any statement of the form (using POSIX regular expressions): "[:alpha:][:alnum:]*=" to be an assignment. Note that this is not technically correct, but it will likely do for your purposes.

    Anyway, at this point, life is easy: scan through the given script, printing everything out as you go. When you come to the occurrence of an assignment (as defined above), print the assignment, ask for a new value, and print that instead of the original value.

    I think that this is rather straightforward, and hopefully has no flaws. Any comments?
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Linux Newbie sdimhoff's Avatar
    Join Date
    Jan 2007
    Posts
    191
    I think that Cabhan has it right on, but it also seemed like you were confused about how to have the script ask for new values for the variables. This script also will then write the values to a file called "a.txt". Here would be a simple example for you:

    Code:
    #!/bin/bash
    #We start with some default values
    mycol=red
    mynum=10
    
    aff='yes'
    
    #Here we include a simple promt for the user
    echo 'Do you want to change color and number? (yes/no)'
    
    #The read command will read a single line of input to the variable "response"
    read response
    
    #Now we check for the answer of 'yes' and write the output to a.txt
    if [ "${response}" = "${aff}" ]
    then
     rm -f a.txt 
     echo 'Enter Color'
     read mycol
     echo $mycol >> a.txt
     echo 'Enter Number'
     read mynum
     echo $mynum >> a.txt
    else
     echo 'No Changes Made'
    fi

  6. #6
    Just Joined!
    Join Date
    Jun 2007
    Posts
    11
    Hi.. Thanks to you all.... for helping me out.. but I think.. my question is still a bit different:

    a.txt, execute, update are all different files...

    My purpose is that ..

    In file a.txt, values of color and boys are already defined as red and 10.
    Now there is a script i.e. execute.. which asks for the values of color and boys and then update it in the file called a.txt

    Now there is one more script called update, which when runs uses the value of color and boys from a.txt and then do some calculations.

    Hope I am clear this time..

  7. #7
    Linux Newbie
    Join Date
    Jan 2008
    Location
    UK
    Posts
    211
    Hi again, I have been giving it some thought and come up with something you maybe could use as a starter. In perl - the script uses hashes which as you can see have a key and a value. so as a result you can manipulate read write sort etc.

    The script gets you to enter values then writes them to the file.
    then by getting the values you can execute your calculations on them.
    see what you think.

    #!/usr/bin/perl
    $file = "test2.txt";
    open my $in, '<', $file or die "Can't read old file: $!";
    open my $out, '>', "$file.new" or die "Can't write new file: $!";

    print "define a hash, enter numbers for green, red, blue\n:";
    $num1 = <stdin>;
    $num2 = <stdin>;
    $num3 = <stdin>;
    # DEFINE A HASH
    &#37;color = ( "green" , $num1,
    "red" , $num2,
    "blue", $num3 );


    # LOOP THROUGH IT
    while (($key, $value) = each(%color)){

    print $out $key.", ".$value."\n";

    }
    close $out;

    wowbag1

Posting Permissions

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