LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2013, 11:54 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
bash case like listing for perl


I have this script - what it does is list all the tmux sessions present on a host. The it presents them in a way that i cah cut and paste the command on to the command line.

Code:
#!/usr/bin/perl -w
open FILE, "/usr/bin/tmux list-sessions|";
while (<FILE>) {
$line = $_;
   if ($line =~ /(\w+):*?/) {
      print "tmux attach-session -t $1\n";
   }script
}
What i need is - i need the tmux sessions listed in a numbered fashion as an output to the script - something that will say 'press 1 for this session' , press 2 for that session' , 'press 3 for the third session'

sort of like the case statement in bash - except dynamic - interactive.
can anybody point me to soemthing - a tutorial. I dont even know where to start.
can anyone point me to an example.
 
Old 10-09-2013, 12:43 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

I'm also a tmux user and I thought that such a script would be very useful, so I went ahead and wrote one in bash. Here it is:
Code:
#!/bin/bash
IFS=$'\n'
declare -a a=($(tmux list-sessions))
[ ${#a[@]} -lt 1 ] && exit 0
for (( i=0 ; i< ${#a[@]}; i++ )) ; do
    echo "Press $i for ${a[$i]}"
done
read j
exec tmux attach-session -t ${a[$j]%%:*}
Things to note:
- line 2: Set IFS to newline so that each entry in the array is a full line not a word.
- line 3: Fill the array a, with the session information
- line 4: Exit if there are no tmux sessions
- last line: Strip everything after the first ":" so that just the session name is passed to "tmux attach-session -t"

HTH,

Evo2.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash: Checking for lower case and upper case letters in a string fatalerror0x00 Programming 1 12-09-2012 02:17 AM
Listing directories with perl program jmc1987 Programming 2 11-17-2011 03:40 AM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
date listing in perl bharatbsharma Programming 1 06-01-2010 11:55 PM
HELP: PERL script to retrieve file listing xboxter Programming 2 05-19-2005 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration