Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

0 votes
1 answers
150 views
Why does a Bash script with select exit without input when reading from stdin?
I'm working on a Bash script that optionally accepts input from stdin and then presents the user with a selection menu using select. The issue arises when the script is provided data via stdin—the select menu displays but exits immediately without accepting any input. It works fine when no stdin dat...
I'm working on a Bash script that optionally accepts input from stdin and then presents the user with a selection menu using select. The issue arises when the script is provided data via stdin—the select menu displays but exits immediately without accepting any input. It works fine when no stdin data is provided. Here is a minimal example: #!/usr/bin/env bash if [[ -p /dev/stdin && ${#bar[@]} -eq 0 ]]; then while IFS= read -r foo; do bar+=("$foo") done
jesse_b (41447 rep)
Oct 5, 2023, 06:54 PM • Last activity: Dec 19, 2024, 07:26 PM
10 votes
6 answers
23532 views
bash - How can I re-display selection menu after a selection is chosen and performed
I am making a tool script for my theme with 2 functions: Check for update, reinstall theme So here is the code for selection menu: PS3='Choose an option: ' options=("Check for update" "Reinstall theme") select opt in "${options[@]}" do case $opt in "Check for update") echo "Checking update" ;; "Rein...
I am making a tool script for my theme with 2 functions: Check for update, reinstall theme So here is the code for selection menu: PS3='Choose an option: ' options=("Check for update" "Reinstall theme") select opt in "${options[@]}" do case $opt in "Check for update") echo "Checking update" ;; "Reinstall theme") echo "Reinstalling" ;; *) echo invalid option;; esac done When running it appear like this 1) Check for update 2) Reinstall theme Choose an option: I type 1 and enter, the check for update command is performed The problem is when it finished performing the script, it re-display "Choose an option:" not with the menu. So it can make users hard to choose without the menu (especially after a long script) 1) Check for update 2) Reinstall theme Choose an option: 1 Checking update Choose an option: So how can I re-display the menu after an option is performed
superquanganh (4041 rep)
Jul 1, 2016, 04:39 PM • Last activity: May 25, 2024, 07:15 PM
3 votes
4 answers
3172 views
How do I select an array to loop through from an array of arrays?
``` #!/usr/bin/bash ARGENT=("Nous devons économiser de l'argent." "Je dois économiser de l'argent.") BIENETRE=("Comment vas-tu?" "Tout va bien ?") aoarrs=("${ARGENT}" "${BIENETRE}") select arr in "${aoarrs[@]}"; do for el in "${arr[@]}"; do echo "$el" done break done ``` I want this script...
#!/usr/bin/bash


ARGENT=("Nous devons économiser de l'argent."
"Je dois économiser de l'argent.")

BIENETRE=("Comment vas-tu?" "Tout va bien ?")

aoarrs=("${ARGENT}" "${BIENETRE}")

select arr in "${aoarrs[@]}"; do
  for el in "${arr[@]}"; do
    echo "$el"
  done
  break
done
I want this script to print the array names to the user, ARGENT and BIENETRE, so that the user can select one of them. After the user's input the script is meant to print every element of an array selected. I want to select with select an array to loop through from an array of arrays (aoarrs). The reason why I want to use select is because in the real world my array of arrays may have many more than just two arrays in it. How might I accomplish that?
John Smith (827 rep)
Aug 28, 2023, 09:10 AM • Last activity: Aug 29, 2023, 05:36 AM
0 votes
1 answers
35 views
how to write a commant for output of a particular group?
> Reading file prod1.tpr, VERSION 2019.6 (single precision) > Reading file prod1.tpr, VERSION 2019.6 (single precision) > Select a group of reference atoms and a group of molecules to be ordered: > Group 0 ( System) has 56000 elements > Group 1 ( Other) has 56000 elements > Group 2 ( DPPC) has 26000...
> Reading file prod1.tpr, VERSION 2019.6 (single precision) > Reading file prod1.tpr, VERSION 2019.6 (single precision) > Select a group of reference atoms and a group of molecules to be ordered: > Group 0 ( System) has 56000 elements > Group 1 ( Other) has 56000 elements > Group 2 ( DPPC) has 26000 elements > Group 3 ( TIP3) has 30000 elements > Group 4 ( Up_Choline) has 2000 elements > Group 5 ( Low_Choline) has 2000 elements > Group 6 ( Up_PO4) has 500 elements > Group 7 ( Low_PO4) has 500 elements > Group 8 ( Up_Glycerol) has 1100 elements > Group 9 ( Low_Glycerol) has 1100 elements > Select a group: Is this possible to give the input for the particular group inside the shell script So no need to select a number for a particular group in the terminal ? If yes then how will be the format to select a particular group in shell script?
PRAMOD KUMAR (1 rep)
Jun 4, 2023, 05:07 PM • Last activity: Jun 4, 2023, 06:03 PM
0 votes
2 answers
383 views
when read buffer size is 2048, it work , but buffer size is 48 or other number, write call will be blocked, why?
``` #include #include #include #include #include #include int main(int argc, char* argv[]){ if(argc < 2){ puts("err argv"); return -1; } int r_fd = open(argv[1], O_RDWR); int w_fd = open(argv[1], O_RDWR); fd_set r_set; fd_set w_set; char w_buf[4096]; char r_buf[2048]; int read_count = 0,write_count...
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char* argv[]){
    if(argc < 2){ 
        puts("err argv");
        return -1; 
    }   

    int r_fd = open(argv, O_RDWR);
    int w_fd = open(argv, O_RDWR);

    fd_set r_set;
    fd_set w_set;
    char w_buf;
    char r_buf;
    int read_count = 0,write_count = 0;  
    while(1){
        FD_ZERO(&r_set);
        FD_ZERO(&w_set);
        FD_SET(r_fd, &r_set);
        FD_SET(w_fd, &w_set);
        int ret = select(w_fd + 1, &r_set, &w_set, NULL, NULL);
        if(FD_ISSET(r_fd, &r_set)){
            read(r_fd, r_buf, sizeof(r_buf));    
            printf("read count:%d\n", read_count++);    
        }   
        if(FD_ISSET(w_fd, &w_set)){
            write(w_fd, w_buf, sizeof(w_buf));
            printf("write count:%d\n", write_count++);  
        }   
        //sleep(1);
    }   

    return 0;
}
execute code:mkfifo 1.fifo && gcc main.c -o main && ./main 1.fifo
zhenfei ren (1 rep)
Feb 9, 2023, 03:52 AM • Last activity: Feb 10, 2023, 12:34 AM
2 votes
2 answers
857 views
select from a constructed list of strings with whitespace?
I trying to create a list of strings with spaces in, that I want to choose between in a `select` - something like this: ``` sel="" while read l do sel=$(printf "%s '%s'" "$sel" "$l") done< <(cd /some/data/directory;du -sh *) select x in $sel do break done ``` The string `sel` looks like expected: `"...
I trying to create a list of strings with spaces in, that I want to choose between in a select - something like this:
sel=""
while read l   
do
  sel=$(printf "%s '%s'" "$sel" "$l")
done< <(cd /some/data/directory;du -sh *) 

select x in $sel
do
  break
done
The string sel looks like expected: "597G 2022" "49G analysis" "25K @Recycle", but the select looks like:
1) "597G      3) "49G       5) "25K
2) 2022"      4) analysis"  6) @Recycle"
#?
What I want to achieve is of course something like:
1) 597G 2022
2) 49G  analysis
3) 25K  @Recycle
#?
And more generally, something where you can select between strings built from several data sources in some way. I have looked for inspiration in several places, like here , but it doesn't quite work for my case. ***Edit*** I forgot to mention, this bash is rather old (and I can't update it, sadly):
[admin@CoMind-UniCron ~]# bash --version
GNU bash, version 3.2.57(1)-release (x86_64-QNAP-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
j4nd3r53n (779 rep)
Jan 6, 2023, 11:20 AM • Last activity: Jan 7, 2023, 12:05 PM
3 votes
2 answers
3310 views
select would indicate pipe is readable when there's no data in pipe and write end is closed?
I am reading *The Linux Programming Interface*. From *63.2.3 When Is a File Descriptor Ready?*, it says: > Correctly using `select()` and `poll()` requires an understanding of > the conditions under which a file descriptor indicates as being ready. > SUSv3 says that a file descriptor (with `O_NONBLO...
I am reading *The Linux Programming Interface*. From *63.2.3 When Is a File Descriptor Ready?*, it says: > Correctly using select() and poll() requires an understanding of > the conditions under which a file descriptor indicates as being ready. > SUSv3 says that a file descriptor (with O_NONBLOCK clear) is > considered to be ready if a call to an I/O function would not block, > *regardless of whether the function would actually transfer data*. The key point is italicized: select() and poll() tell us whether an > I/O operation would not block, rather than whether it would > successfully transfer data. In this light, let us consider how these > system calls operate for different types of file descriptors. We show > this information in tables containing two columns: > > - The select() column indicates whether a file descriptor is marked as readable (r), writable (w), or having an exceptional condition (x). > > .... > > Pipes and FIFOs > > Table 63-4 summarizes the details for the read end of a pipe or FIFO. > The Data in pipe? column indicates whether the pipe has at least 1 > byte of data available for reading. In this table, we assume that > POLLIN was specified in the events field for poll(). > > .... > > Table 63-4: select() and poll() indications for the read end of a > pipe or FIFO > > Condition or event | select() | poll() > Data in pipe? | Write end open? | > no | no | r | POLLHUP > yes | yes | r | POLLIN > yes | no | r | POLLIN | POLLHUP > > And Table 63-5: select() and poll() indications for the write end > of a pipe or FIFO > (In this table, we assume that POLLOUT was specified in the events field for poll().) > > Condition or event | select() | poll() > Space for PIPE_BUF bytes? | Read end open? | > no | no | w | POLLERR > yes | yes | w | POLLOUT > yes | no | w | POLLOUT | POLLERR I don't understand the 1st row condition of both tables. No data in pipe, write end closed, select() would indicate that as a readable file descriptor? Why? Shouldn't select() block till there's data in the pipe? No space for PIPE_BUF bytes, read end closed, select() would indicate that as a writeable file descriptor?
Rick (1247 rep)
Jun 10, 2020, 02:28 AM • Last activity: Dec 12, 2022, 12:46 PM
1 votes
1 answers
68 views
Selecting from various media using awk shell script
I have made a simple backup program for my bin folder. It works. Code and resultant STDOUT below. Using rsync to copy from local ~/bin folder to a /media/username/code/bin folder. The code works fine when only one result from `mount | grep media` but I can not quite fathom how to advance it to letti...
I have made a simple backup program for my bin folder. It works. Code and resultant STDOUT below. Using rsync to copy from local ~/bin folder to a /media/username/code/bin folder. The code works fine when only one result from mount | grep media but I can not quite fathom how to advance it to letting me select from multiple results from the mount/grep. I suspect the for LINE below is lucky to work at all as I believe for is delimited by spaces, in shell scripting, but as there are no spaces in the results it then delimited on the \n ? I tried find /media and of course got a *lot* of results. Not the way to go I think. O_O check_media () { FOUNDMEDIA=$(mount | awk -F' ' '/media/{print $3}') echo -e "foundmedia \n$FOUNDMEDIA" echo For Loop for LINE in $FOUNDMEDIA do echo $LINE done CHOSENMEDIA="${FOUNDMEDIA}/code/bin/" echo -e "\nchosenmedia \n$CHOSENMEDIA\n" exit }
bin is /home/dee/bin/
foundmedia 
/media/dee/D-5TB-ONE
/media/dee/DZ61

For Loop
/media/dee/D-5TB-ONE
/media/dee/DZ61

chosenmedia 
/media/dee/D-5TB-ONE
/media/dee/DZ61/code/bin/
You can see how I add the save path /code/bin to the found media but with multiple results I get a chosenmedia which cannot work. I would like to be able to choose the media to which I want to rsync my backup to, or restore from.
Dee (33 rep)
Oct 17, 2022, 10:58 AM • Last activity: Oct 17, 2022, 12:44 PM
12 votes
2 answers
7111 views
Can I change how select options are displayed?
I'm working with select and case in bash. I currently have nine options, which makes a nice, tidy, 3x3 grid of options, but it displays like so: 1) show all elements 4) write to file 7) clear elements 2) add elements 5) generate lines 8) choose file 3) load file 6) clear file 9) exit I'd prefer if i...
I'm working with select and case in bash. I currently have nine options, which makes a nice, tidy, 3x3 grid of options, but it displays like so: 1) show all elements 4) write to file 7) clear elements 2) add elements 5) generate lines 8) choose file 3) load file 6) clear file 9) exit I'd prefer if it displayed in rows before columns: 1) show all elements 2) add elements 3) load file 4) write to file 5) generate lines 6) clear file 7) clear elements 8) choose file 9) exit Is there any way to accomplish this? Preferably something easy to set and unset within a script, like a shell option. If it matters, the options are stored in an array, and referenced in the case blocks by the index of the array. OPTIONS=("show all elements" "add elements" "load file" "write to file" "generate lines" "clear file" "clear elements" "choose file" "exit") ... select opt in "${OPTIONS[@]}" do case $opt in "${OPTIONS}") ... "${OPTIONS}") echo "Bye bye!" exit 0 break ;; *) echo "Please enter a valid option." esac done
RICK (399 rep)
May 14, 2015, 02:42 AM • Last activity: Sep 24, 2022, 05:18 PM
0 votes
1 answers
779 views
Why plus 1 is needed in select system call?
From `man select` : ``` int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); ``` > nfds should be set to the highest-numbered file descriptor in any of the three sets, plus 1. I'm quite curious about : 1. Why `plus 1` is needed, not highest-numbered fi...
From man select :
int select(int nfds, fd_set *readfds, fd_set *writefds,
           fd_set *exceptfds, struct timeval *timeout);
> nfds should be set to the highest-numbered file descriptor in any of the three sets, plus 1. I'm quite curious about : 1. Why plus 1 is needed, not highest-numbered file descriptor itself? 2. Why request plus 1 operation in user input, instead of handling it inside the system? From sys_generic.c, it seems related __NFDBITS, but I'm not able to go further.
static int max_select_fd(unsigned long n, fd_set_bits *fds)
 339{
 340        unsigned long *open_fds;
 341        unsigned long set;
 342        int max;
 343        struct fdtable *fdt;
 344
 345        /* handle last in-complete long-word first */
 346        set = ~(~0UL files);
 349        open_fds = fdt->open_fds->fds_bits+n;
 350        max = 0;
 351        if (set) {
 352                set &= BITS(fds, n);
 353                if (set) {
 354                        if (!(set & ~*open_fds))
 355                                goto get_max;
 356                        return -EBADF;
 357                }
 358        }
 359        while (n) {
 360                open_fds--;
 361                n--;
 362                set = BITS(fds, n);
Similar topic but not the same: https://unix.stackexchange.com/questions/7742/whats-the-purpose-of-the-first-argument-to-select-system-call
whisper (11 rep)
May 5, 2022, 07:20 AM • Last activity: May 5, 2022, 02:55 PM
2 votes
1 answers
8983 views
Getting the first/last/nth result from a jq select result
I am trying to find a way to pipe the result of my current script into another command which will leave me with just the last result of the array I get in my select here: ```bash jq -r --arg name "$1" '.packageAliases | to_entries[] | select(.key | startswith($name))' sfdx-project.json ``` the `sfdx...
I am trying to find a way to pipe the result of my current script into another command which will leave me with just the last result of the array I get in my select here:
jq -r --arg name "$1" '.packageAliases | to_entries[] | select(.key | startswith($name))' sfdx-project.json
the sfdx-project.json file is a json file that has a nested JSON called packageAliases, which a script adds new package version numbers and Ids to.
{
 "data" : "that",
 "is" : "not",
 "really" : "necessary",
 "packageAliases" : {
  "package" : "0H0fffff",
  "package@0.1.0.1" : "04t0xxxxxx"
 }
}
The idea is to select all package aliases of a specific package name, and leave me with the last one, which is the one that I am supposed to use. When I try to pipe the result I get from my select into last, I get this error: Cannot index object with number Although I thought that the select() command returns an array (which I also get when I echo the intermediary result) Placing the last() command anywhere else just leaves me with several null values in an array, but I still do not get the single value I actually need. What do I need to change about my command to get the last entry from the array that I get out of select()?
dschib (51 rep)
Mar 9, 2022, 02:09 PM • Last activity: Mar 9, 2022, 06:52 PM
0 votes
2 answers
205 views
Select command showing options without question
Select command is showing options without displaying question. It is displaying question after a choice is made. I have a function as shown below in a script. function start_AppNode { # Define local variables for all the inputs local DName_l=$1 local ASName_l=$2 local ANName_l=$3 echo "Do you wish t...
Select command is showing options without displaying question. It is displaying question after a choice is made. I have a function as shown below in a script. function start_AppNode { # Define local variables for all the inputs local DName_l=$1 local ASName_l=$2 local ANName_l=$3 echo "Do you wish to start the AppNode $ANName_l on AppSpace $ASName_l in Domain $DName_l ?" select yn in "Yes" "No"; do case $yn in Yes ) #print_log $yn OUTPUT="./appadmin start -d $DName_l -as $ASName_l anode $ANName_l" if [[ "$OUTPUT" == *"$SuccessMessage_AppNode_Start"* ]]; then echo "Started" else echo "Failed" fi break;; No ) echo "Failed"; break;; esac done } When I call that script in an other script where I imported this script, I am getting option first and not showing the question "Do you wish to start the AppNode $ANName_l on AppSpace $ASName_l in Domain $DName_l ?" It shows the question after a choice is being made. See screenshot of output below. Appreciate if anyone can help me understand how to make it show the question first? enter image description here Update: The issue is due to variable assignment. I am assigning the output of the function to a variable. If I execute the function without assigning the return value to a variable, it works. How can I assign the return value to a variable and still have the question part displayed before the options?
Anil_4_Tibco_Java (3 rep)
Dec 6, 2021, 05:20 PM • Last activity: Dec 6, 2021, 09:24 PM
0 votes
2 answers
22427 views
select all text in vi to copy and past in windows
How can I select all text in `vi` so that I can copy it, and then paste it in notepad on my windows server. I have a file with 3,000 lines in it. I cannot use FTP so I need to copy the text in the Linux environment and past it into notepad on windows.
How can I select all text in vi so that I can copy it, and then paste it in notepad on my windows server. I have a file with 3,000 lines in it. I cannot use FTP so I need to copy the text in the Linux environment and past it into notepad on windows.
user289335 (31 rep)
May 30, 2018, 11:28 PM • Last activity: Oct 21, 2021, 06:31 AM
3 votes
2 answers
10987 views
How to select a word under cursor?
For all text editor or application I'm using, I want to select a word under cursor using keyboard shortcut. How can I do it? I tried look around I didn't find anything helpful.
For all text editor or application I'm using, I want to select a word under cursor using keyboard shortcut. How can I do it? I tried look around I didn't find anything helpful.
Swapnil (271 rep)
Aug 20, 2021, 08:42 AM • Last activity: Aug 20, 2021, 09:15 AM
1 votes
3 answers
432 views
TMOUT within script file misbehaves for the select command. How to fix it?
**UPDATE** - looks like this is something peculiar with bash 4.3.42 as it works fine with 4.3.46. Leaving this post for those who run into the same issue in future. When I run this command in bash command line it works properly: % (TMOUT=3; s="no selection"; select s in a b c ; do break ; done; echo...
**UPDATE** - looks like this is something peculiar with bash 4.3.42 as it works fine with 4.3.46. Leaving this post for those who run into the same issue in future. When I run this command in bash command line it works properly: % (TMOUT=3; s="no selection"; select s in a b c ; do break ; done; echo $s) 1) a 2) b 3) c #? no selection % _ Result: displays *no selection* and comes back to command line. When, however, I place it a script, and execute it, it repetitively requests selection. % cat a.sh #!/bin/bash (TMOUT=3; s="no selection"; select s in a b c ; do break ; done; echo $s) % ./a.sh Result: 1) a 2) b 3) c #? 1) a 2) b 3) c #? 1) a 2) b 3) c #? ^C % _ Why it so? My main question is - **how to make it work in a script?!** **UPDATED** % bash --version GNU bash, version 4.3.42(1)-release (x86_64-unknown-linux-gnu) Copyright (C) 2013 Free Software Foundation, Inc. % uname Linux lx1 2.6.32-642.6.2.el6.x86_64 #1 SMP Mon Oct 24 10:22:33 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
Grzegorz (121 rep)
Jun 7, 2019, 06:58 PM • Last activity: Sep 5, 2020, 04:32 PM
0 votes
1 answers
1817 views
Bash script selecting folder
I am trying to list all the directory latest modified folder first using `select`, but I am stuck. Let's say I have: Folder1 ThisIsAnotherDir Directory New Directory This IS not_Same Directory When I run following command: ls -t -d */ I get the desired output. But when I use `select`: options=$(ls -...
I am trying to list all the directory latest modified folder first using select, but I am stuck. Let's say I have: Folder1 ThisIsAnotherDir Directory New Directory This IS not_Same Directory When I run following command: ls -t -d */ I get the desired output. But when I use select: options=$(ls -t -d */) select d in $options; do test -n "$d" && break; echo ">>> Wrong Folder Selection!!! Try Again"; done It lists the folder modified first, but, if I modified New Directory last and run this, it outputs: 1)New 2)Directory 3)Folder1 4)ThisIsAnotherDir 5)Directory 6)This 7)IS 8)not_Same 9)Directory I also tried: select d in "$options"; do test -n "$d" && break; echo ">>> Wrong Folder Selection!!! Try Again"; done It fails also. I hope this make sense. Thank you
KpDean (3 rep)
Sep 1, 2020, 07:25 AM • Last activity: Sep 1, 2020, 04:39 PM
3 votes
2 answers
2342 views
Bash ignoring SIGINT trap when 'select' loop is running
When i use 'trap' combined with select loop, namely when i try to hit CTRL+C to break out while options are displayed, it will just print ^C in terminal. If i remove 'trap' from script it will normally exit out, that is it will accept CTRL+C. I've tested this on two different versions of bash (One s...
When i use 'trap' combined with select loop, namely when i try to hit CTRL+C to break out while options are displayed, it will just print ^C in terminal. If i remove 'trap' from script it will normally exit out, that is it will accept CTRL+C. I've tested this on two different versions of bash (One shipped with CentOS and one shipped with Fedora), and i have an issue with the one from Fedora (4.4.23(1)-release). Bash version 4.2.46(2)-release that is shipped with CentOS seems to work fine. I've also tested this on local terminal and remote (via ssh). And problem is always on Fedora side. I will post code to see what I'm talking about This one doesn't work: #!/bin/bash trap exit SIGINT select opt in One Two Three; do break done If i were to remove the entire 'trap exit SIGINT' line, it will work fine and accept CTRL+C without issues. Any ideas how to fix or bypass this ?
Marko Todoric (437 rep)
Apr 19, 2019, 08:41 PM • Last activity: Apr 10, 2020, 06:43 PM
0 votes
0 answers
367 views
Xed : highlight selected text occurences?
I'm using Gnome Xed text editor. Is there a way to auto select all occurrences of currently selected-text across the document. If Xed cant do it, what other linux text editor can do it ? How ?
I'm using Gnome Xed text editor. Is there a way to auto select all occurrences of currently selected-text across the document. If Xed cant do it, what other linux text editor can do it ? How ?
sten (103 rep)
Feb 26, 2020, 01:47 AM
2 votes
0 answers
196 views
Bash menu from screen -ls data
Given the screen data There are screens on: 27454.world-of-dragons_SERVER (07/05/2018 04:38:56 PM) (Attached) 6223.potato-wod_SERVER (07/05/2018 10:16:12 AM) (Attached) 1681.potato-wod_MASTER (07/04/2018 10:06:20 PM) (Detached) 30448.world-of-dragons_MASTER (07/04/2018 09:06:01 PM) (Detached) 4 Sock...
Given the screen data There are screens on: 27454.world-of-dragons_SERVER (07/05/2018 04:38:56 PM) (Attached) 6223.potato-wod_SERVER (07/05/2018 10:16:12 AM) (Attached) 1681.potato-wod_MASTER (07/04/2018 10:06:20 PM) (Detached) 30448.world-of-dragons_MASTER (07/04/2018 09:06:01 PM) (Detached) 4 Sockets in /var/run/screen/S-kreezxil. I have come up with a script that generates a menu for the Detached consoles.
#!/bin/bash

declare -a pids
declare -a names

build_arrays() {
        IDX=0
        pids=()
        names=()

        for f in $(screen -ls); do
                if [[ $f = *"MASTER"* ]]; then
                        IFS="." read -r -a data <<< "$f"
                        pids[IDX]="${data}"
                        IFS="_" read -r -a name <<< "${data}"
                        names[IDX]="${name}"
                        ((++IDX))
                fi
        done
}

declare -p

while true; do
        build_arrays

        size=${#pids[@]}

        clear
        printf '\nSCREEN\tPID\tNAME\n'

        for (( i=0; i<${size}; i++ )); do
                printf '%s\t%s\t%s\n' ${i} ${pids[i]}  ${names[i]}
        done

        printf '\nWhich screen # would you like to resume?\nEnter to q to quit or exit\n'
        read n

        if [ $n == 'q' ]; then
                exit
        fi

        if [ $n -lt 0 || $n -gt $size ]; then
                read -p "Invalid Option: Press [Enter] to try again" readEnterkey
                continue
        fi

        screen -r ${pids[$n]}
done
## The Question Can this script be made more elegant and human readable? ## Edit 10/20/2019: Updated the script to rebuild it's arrays in the event that one of the screens are destroyed or other screens get added. Suggestions: welcomed.
Kreezxil (75 rep)
Jul 5, 2018, 07:25 PM • Last activity: Jan 25, 2020, 09:19 PM
2 votes
1 answers
594 views
How to display different directory content with Bash select command?
So far I have found some examples of Bash [select][1] function usage with [logical constructed options][2] or [Asterisk one][3] (i.e `select s in *`). *This last one lists all actual directory content.* **So I would like to understand the following:** 1. What does `*` mean? 2. How could I display an...
So far I have found some examples of Bash select function usage with logical constructed options or Asterisk one (i.e select s in *). *This last one lists all actual directory content.* **So I would like to understand the following:** 1. What does * mean? 2. How could I display another directory content? *(I tried something as opt=$( ls path ) and select s in "$opt[@]", but it fails)* Any related links or examples are welcome.
artu-hnrq (327 rep)
Nov 21, 2019, 07:13 AM • Last activity: Nov 21, 2019, 02:44 PM
Showing page 1 of 20 total questions