Find the answer to your Linux question:
Results 1 to 7 of 7
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Reading reverse input


    Code:
    #!/bin/bash
    
    let $pointer = 0
    let $value = 0
    
    echo "Input value"
    
    read uinput
    
    # At this point, I want to read the user input in reverse order, with the last character entered being read first until all characters are read. Set the pointed value to $input
    # Trouble is, I'm not sure how to account for a variable input. I know I can use 
    #set n=1 
    #read $n 
    #n++ 
    to read left to right, but I need to read right to left
    
    #loop until there are no values left to check
    
    until [[ -z "$pointer" ]] ; do
    
    #Assign the currently pointed value of the users input to $input
    
    let @${"$pointer"}="$input"
    
    # check for a valid input.(any number, or a-f) If invalid, terminate
    
    if "$input"="[0-9][a-f]" ; then
       continue
    else
       echo "That value is invalid"
       exit
    fi
    
    #Translate alpha to numerics if needed
    if "$input"="a" ; then 
       let $input = 10
    elif "$input"="b" ; then
       let $input = 11
    elif "$input"="c" ; then
       let $input = 12
    elif "$input"="d" ; then
       let $input = 13
    elif "$input"="e" ; then
       let $input = 14
    elif "$input"="f" ; then
       let $input = 15
    fi
    
    # add the current input to the main variable
    
       let $value = $value + $input
    
    # adjust the pointer to look at the previous value in line
    
       let $pointer = $pointer - 1
    
    # If the value of the pointer is null, end. Else *16 and continue
    
       if [[ -z "$pointer" ]] ; then
            echo "$value"
       else
           let $value = $value*16
       fi
       
    done
    Trying to code a basic algorithm here. To solve, you take the last value and add it to the main sum, then if there are any values before the one you just added, multiply by 16 before adding that value. Loop until complete.
    Last edited by RTiger; 4 Weeks Ago at 08:38 AM.

  2. #2
    please, use code tags for code: [code]your script here[/code]

    before even looking at what that script does, i see so many errors... please run it through
    https://www.shellcheck.net/
    repeatedly.

    what i see so far:
    read uinput, not $uinput
    until ... requires a then, either after a semicolon; on the same line, or on the next line.
    same for if
    ...
    If a post is too long and unstructured I might miss important information.
    How to ask smart questions | You can post a link by removing "http://www." from it!

  3. #3
    Ok, that should clean it up. Thanks for the link to check my code.

    Still have the same question though, how to read user input in reverse order.

    Let me give an example of what I want this to do.

    uinput is 3154

    I would want $pointer in this case to be set to 4. Start with 4 as $input ($4) Add to $value (4) ... oh, I need to set the terminate condition to $pointer=0 in this case. a uinput of 3154 becomes the problem 4*16+5*16+1*16+3 in linear order.

    however, if uinput was instead 4b27af, I would want $pointer to be 6, and the equation would be 15*16+10*16+7*16+2*16+11*16+4. if uinput was dog, the program would error out.

    hmm, alternatively, if I can get the program to reverse the read order, I can just use 1+1 and search for a null value.
    Last edited by RTiger; 4 Weeks Ago at 09:58 PM.

  4. $spacer_open
    $spacer_close
  5. #4
    And now I realize my math is completely incorrect. The question still stands though.

  6. #5
    hi,

    you can't read user input in reverse order; you read it as it's given !

    you might transform user's input so it gets reversed :
    Code:
    for ((i=${#1};i>=0;i--)); do out+="${1:$i:1}"; done;echo $out;
    instead of putting each char into a variable, you could use it for what ever you want.

  7. #6
    Quote Originally Posted by nihili View Post
    until ... requires a then, either after a semicolon; on the same line, or on the next line.
    same for if
    ...
    sorry, this should read:
    until ... requires a do... if ... requires a then...
    also, please do not edit your first post after it has been replied to, especially not when you change the original meaning.
    If a post is too long and unstructured I might miss important information.
    How to ask smart questions | You can post a link by removing "http://www." from it!

  8. #7
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,418
    Hi.

    As noted earlier:
    Quote Originally Posted by watael View Post
    hi,

    you can't read user input in reverse order; you read it as it's given !

    you might transform user's input so it gets reversed ...
    Here are some other means to reverse objects in file:
    Code:
    Reverse lines [ or paragraphs, words, characters ] in file
    
            1) my-tac (local), reverse characters, words, lines, paragraphs
    
            2) tac, often included with standard commands
    
            3) Print strings [tokens] for each line in reverse order:
               perl -wn -e '@a=split; print join(" ",reverse @a),"\n";' file
    
            4) tail -r file
    
            5) sed '1!G;h;$!d' file
    
            6) Read file backwards:
               perl module File/ReadBackwards.pm (requires coding)
    
            7) rev - reverse lines characterwise
               repository, or:
               ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/
    
            8) print CLI parameter strings [tokens] in reverse order:
               perl -w -e 'print join(" ", reverse @ARGV),"\n";' s1 s2 ... sn
               results: sn ... s2 s1
    
            9) revfields - reverses the order of fields on every line.
               part of "missing utils":
               http://www1.cuni.cz/~obo/textutils/ (2016.08)
    
            10) datamash reverse ( text and numeric; delimited STDIN; allows headers )
    If you absolutely needed to develop the algorithm you mentioned, you could read a single character at a time in a loop, and that way the first character you read would be the one that corresponded to the left-most character in a string. So ...
    Code:
          -n nchars return after reading NCHARS characters rather than waiting
                    for a newline, but honor a delimiter if fewer than NCHARS
                    characters are read before the delimiter
          -N nchars return only after reading exactly NCHARS characters, unless
                    EOF is encountered or read times out, ignoring any delimiter
    ...
    excerpt from man bash, built-in command read

    Best wishes... cheers, drl
    Last edited by drl; 4 Weeks Ago at 10:47 AM. Reason: Addition for reading
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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