LinuxQuestions.org
Help answer threads with 0 replies.
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 07-05-2013, 11:26 AM   #1
manuel19
Member
 
Registered: May 2010
Distribution: Fedora Core 8 (Werewolf)
Posts: 40

Rep: Reputation: 0
How to create a ____________ in 100% in shell script?


Hi, I'm creating a shell script.
I need a way to create a Line 100% Width like menus.

Ex:



___________________________________(100%width)
My Application title
___________________________________(100%width)

1 - For exit.
2 - Read
...
..
..




How can i do that?
 
Old 07-05-2013, 01:03 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Something like this is not going to be easily done just within BASH. I would recommend perl for something like this.

Here is CPAN module that would provide the progress bar you are looking for.

http://search.cpan.org/~szabgab/Term...ProgressBar.pm

Perl also allows you to create menus easily enough.

You could try and use curses with bash but not sure if that would give you what you are looking for easily:

http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/
 
Old 07-05-2013, 01:41 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
The COLUMNS environment variable holds the terminal width, so something like:
Code:
for ((i=0;i<$COLUMNS;i++)); do echo -n _; done
Would print a line the whole width of the terminal.
 
2 members found this post helpful.
Old 07-05-2013, 05:28 PM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 07-06-2013, 05:54 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
My first question would be, 100% of what?
 
Old 07-06-2013, 01:23 PM   #6
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,658

Rep: Reputation: 255Reputation: 255Reputation: 255
#!/bin/bash colmax=$(tput cols) lig=$(tput lines)

tput gives you the cols/lines

To count, you may use:
cat "$1" | awk 'END {print NR}'

and so on.


I would be you, I would learn coding in C language. ask me, I can guide you through ur learning

apt-get install gcc
and also ncurses, that will help
 
Old 07-07-2013, 04:08 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
___________ in 100%????

I'm not sure what you really want but I think you could make use of the dialog command. And if you want to generate a line you just make use of echo with the use of substring expansion I think. That's less expensive compared to multiple calls to echo in a loop.
Code:
LINE=__________
for (( I = 1; I <= 5; ++I )); do  ## or "for {1..5}; do" in newer Bash probably 4.0
    LINE=${LINE}${LINE}  # or LINE+=${LINE} in Bash 3.1+
done

echo "${LINE:0:100}"  ## Change 100 to any number depending on the needed line's length
echo "My Application title"
echo "${LINE:0:100}"
 
Old 07-07-2013, 04:37 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
printf -v line '%.*d\n' "$COLUMNS"
echo "${line//0/_}"
"*" is a printf format feature that gets substituted by the numerical value of the first non-format argument.

You only need to ensure that the COLUMNS variable or similar is available.

I'm trying to get the number of columns or lines of my terminal but the variables COLUMNS / LINES are always empty
http://mywiki.wooledge.org/BashFAQ/091

Finally, as printed above it requires bash/ksh features, but it could be replicated in other shells with command substitution and something like tr.
 
Old 07-07-2013, 05:49 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Just consider that printf -v is a new feature available to versions of Bash not earlier than 4.0 so consider using it if compatibility to earlier versions is not necessary.

As for Xeratul's suggestion to tput, you could also have stty to get the number of columns:
Code:
read LINES COLUMNS < <(exec stty size)
echo "$COLUMNS"
Putting those two together we could have something like:
Code:
#!/bin/bash

shopt -s extglob

if [[ $(type -t tput 2>/dev/null) == file ]] && read COLUMNS < <(exec tput cols) && [[ $COLUMNS == +([[:digit:]]) && COLUMNS -gt 0 ]]; then
    :
elif [[ $(type -t stty 2>/dev/null) == file ]] && read __ COLUMNS < <(exec stty size) && [[ $COLUMNS == +([[:digit:]]) && COLUMNS -gt 0 ]]; then
    :
else
    echo "Unable to find number of columns for the screen."
    exit 1
fi

# Do something here ...
 
Old 07-07-2013, 06:11 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Actually -v has been available since 3.1, although it was expanded to support array elements in 4.1*.

http://wiki.bash-hackers.org/scripting/bashchanges

But if you don't have it for some reason, just capture the output instead.

Code:
line=$( printf '%.*d\n' "$COLUMNS" )


(*Still only a single fixed entry though. But wouldn't it be great if it could set an entire array, with one element per iteration?)
 
Old 07-07-2013, 06:58 AM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by David the H. View Post
Actually -v has been available since 3.1, although it was expanded to support array elements in 4.1*.
Yes you're correct. Sorry I misread an online post.
Quote:
printf '%.*d\n' "$COLUMNS"
Yes it wouldn't be a bad idea to use it over the initial iteration.

Last edited by konsolebox; 07-07-2013 at 07:03 AM.
 
  


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
How to generate shell script for 1 3 2 4 3 5 4 6..100 learngeek Linux - Newbie 5 12-06-2012 04:25 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
print 1 to 100 in a shell script sleeper0110 Programming 5 11-02-2008 04:55 AM
how can create shell script user52 Linux - Newbie 4 11-27-2006 11:16 AM
how create shell script user52 Linux - General 1 11-27-2006 03:35 AM

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

All times are GMT -5. The time now is 08:31 AM.

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