LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   reading variable while remaining on the same line - bash (https://www.linuxquestions.org/questions/programming-9/reading-variable-while-remaining-on-the-same-line-bash-547797/)

GSMD 04-21-2007 03:59 AM

reading variable while remaining on the same line - bash
 
Here's a function that allows create simple user accounts in a htpasswd file.
Code:

                                        printf "\nThe team has the following members:\n"
                                        cat $VAR_TEAM_ROOT/auth | cut -d: -f1
                            printf "\nEnter member with password to add:\n"
                        # until get an empty member name or password
                        MEMBER=1
                        PASSWORD=1
                        until [ -z "$MEMBER" ] || [ -z "$PASSWORD" ]
                        do
                        read MEMBER PASSWORD
                                LISTING_TEST "$VAR_TEAM_ROOT/auth" "$MEMBER"
                                      if [ "$?" = 1 ]
                                              then
                                                      printf "[\e[1;32m+\e[m]\n"
                                                      # add to auth file
                                                htpasswd -b $VAR_TEAM_ROOT/auth $MEMBER $PASSWORD &> /dev/null
                                                # set trac permissions
                                                                        trac-admin $VAR_TEAM_ROOT/trac permission remove "$MEMBER" &> /dev/null
                                                trac-admin $VAR_TEAM_ROOT/trac permission add "$MEMBER" developer &> /dev/null
                                        else
                                                printf "[\e[1;31m-\e[m]\n"
                                fi
                        done

LISTING_TEST verifies the value entered against the list of users already in the file and returns "1" in case user is not found (what we basically need). In this case user gets added to the file and a "[+]" sign is printed on the next line.
The question is: how can I make the sign appear on the same line name and pass are displayed?
So that it would look like
user password [+]

Just out of ideas :scratch:. Thanks.

druuna 04-21-2007 05:28 AM

Hi,

This can probably be done with tput

Code:

#!/bin/bash

read MEMBER PASSWORD

tput cuu1
tput cuf 35
printf "[\e[1;32m+\e[m]\n"

You will probably need to do some arithmetic to calculate the exact col position (cuf 35 in my example). Or you could decide to give it a fixed width (as in my example).

Here are 3 related URL's
Linux / Unix Command: tput
Colours and Cursor Movement With tput
(example) The Floating Clock Prompt

Hope this helps.

GSMD 04-21-2007 05:52 AM

Awesome!
Though it looks a bit cryptic, works like charm. I'll study more on tput, thanks for the reference.

kitoper 04-24-2011 09:47 AM

rudimentary read tag
 
Read -p "insert text here" variable_here


All times are GMT -5. The time now is 03:56 PM.