LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-01-2017, 11:31 PM   #1
alberg
Member
 
Registered: Jun 2016
Distribution: Arch, Slackware
Posts: 47

Rep: Reputation: Disabled
Shell script doesn't clear temporary output upon exit


Greetings!

There is a shell script called 'fasd' which takes several letters of a directory name, displays in a temporary menu a list of matching directory full names, reads a number of selected item from user and cd's to the selected directory.

The temporary menu was disapperaring from the screen until recently. I'm using Arch and after a recent system upgrade the script changed it's behaviour and the temporary menu is not disappearing anymore after script's exit. The script itself was not updated, so I guess an update in some library has caused this change.

My question is: what is the shell mechanism which was causing the temporary menu to be cleared on exit in that script?

The script can be seen here:

https://github.com/clvv/fasd/blob/master/fasd

It's rather convoluted, so below is an approximate description of it's working in the part relevant to the temporary menu.

The script is called with 'zz <dirname>' command. 'zz' is an alias to the 'fasd_cd' shell function.

1. The shell function fasd_cd, takes the argument and calls the script with it and additionaly with '-e printf %s' parameter which means simply to print the result to stdout on exit. (This function takes one argument which is a shortened (fuzzy) name of a directory to cd into.)

2. Script queries its directory database with the fuzzy directory name and prints the query result, which is a list of full directory names similar to the fuzzy one, as a temporary menu and reads the user's input with a menu item number:
Code:
res="$(printf %s\\n "$res" | sort -n${R})"
printf %s\\n "$res" | sed = | sed 'N;s/\n/	/' | sort -nr >&2
printf "> " >&2
local i; read i; [ 0 -lt "${i:-0}" ] 2>> /dev/null || return 1
res="$(printf %s\\n "$res" | sed -n "${i:-1}"'s/^[^ ]*[ ]*//p')"
3. It prints the selected line with the full directory name and exits (returns to the bash 'fasd_cd' function).

4. The fasd_cd takes the printed result and executes the 'cd' command with it. The relevant function's code looks as follows:

Code:
local _fasd_ret="\$(fasd -e 'printf %s' "\$@")"
[ -d "\$_fasd_ret" ] && cd "\$_fasd_ret"
What shell mechanism caused the menu printed in #2 to be cleared on the 'fasd_cd' exit?

Thanks.
 
Old 01-02-2017, 05:57 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,509

Rep: Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656
Why not simply add a
Code:
clear
command where you want the screen to clear just before exit?
 
Old 01-02-2017, 06:28 AM   #3
alberg
Member
 
Registered: Jun 2016
Distribution: Arch, Slackware
Posts: 47

Original Poster
Rep: Reputation: Disabled
This will clear the previous content of the screen, which remained intact when the menu was disappearing.
 
Old 01-02-2017, 01:05 PM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,509

Rep: Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656
Quote:
Originally Posted by alberg View Post
This will clear the previous content of the screen, which remained intact when the menu was disappearing.
I see, it is not just de-cluttering the display, you want to see the original stuff?

I would look into the utils and moreutils. "dialog" and "whiptail" come to mind, but I am not sure that they will serve. Having the script open a new terminal for display, and closing it on exit might do, but I have not tested the technique.
 
Old 01-02-2017, 07:37 PM   #5
alberg
Member
 
Registered: Jun 2016
Distribution: Arch, Slackware
Posts: 47

Original Poster
Rep: Reputation: Disabled
That's good idea, but if to look into the original solution: there is thing in Unix terminals called "alternate display", which causes the program's output to be cleared after it's exit and to restore the previous terminal's screen. This behaviour can be triggered by a command ot terminal sequence:

http://stackoverflow.com/questions/1...-a-bash-script

In that script (fasd) I don't see any similar command is called anywhere. So maybe some default settings somewhere were such, that the temporary script's output was cleared after exit, and then maybe some defaults somewhere were changed?
 
1 members found this post helpful.
Old 01-03-2017, 05:42 AM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,509

Rep: Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656Reputation: 2656
Quote:
Originally Posted by alberg View Post
That's good idea, but if to look into the original solution: there is thing in Unix terminals called "alternate display", which causes the program's output to be cleared after it's exit and to restore the previous terminal's screen. This behaviour can be triggered by a command ot terminal sequence:

http://stackoverflow.com/questions/1...-a-bash-script

In that script (fasd) I don't see any similar command is called anywhere. So maybe some default settings somewhere were such, that the temporary script's output was cleared after exit, and then maybe some defaults somewhere were changed?
I had not seen that article, that is very interesting.
Why not try wrapping your script output in those tput lines to get that effect? Have you tried that yet?
 
Old 01-03-2017, 06:03 AM   #7
alberg
Member
 
Registered: Jun 2016
Distribution: Arch, Slackware
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
Why not try wrapping your script output in those tput lines to get that effect?
I tried to run this in bash (xfce4-terminal):

Code:
[al tst ]$ tput smcap;echo "Some output....";sleep 3;tput rmcap
tput: unknown terminfo capability 'smcap'
Some output....
tput: unknown terminfo capability 'rmcap'
Is this correct way of using this commands? No alternate screen was appeared.

EDIT:

Using escape sequences made some effect:

Code:
echo -e "\e[?1049h";echo "Some output...."; echo -e "\e[?10491"
This has cleared the screen, printed the "Some output...", but didn't return to the previous screen content:

Code:
Some output....

[?10491
[al tst ]$

Last edited by alberg; 01-03-2017 at 06:22 AM.
 
Old 01-03-2017, 06:10 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by alberg View Post
http://stackoverflow.com/questions/1...-a-bash-script

In that script (fasd) I don't see any similar command is called anywhere. So maybe some default settings somewhere were such, that the temporary script's output was cleared after exit, and then maybe some defaults somewhere were changed?
it says right there in the accepted answer that you can use escape sequences to achieve the same.
checked for those (be aware; there's different ways to integrate them in a script; best if youjust search for '1049h')?

edit:
your edit:
well, you (your term) don't have that ability (man termcap).
if fasd works as intended, it achieves the same some other way.
maybe ncurses?

Last edited by ondoho; 01-03-2017 at 06:12 AM.
 
Old 01-03-2017, 06:11 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
maybe use more variables to hold the values you want to keep, then print them out as needed. Maybe using the 'clear' command to clear the screen then a function call to "print" everything you need back to the screen when you need it placed there.
 
Old 01-03-2017, 06:27 AM   #10
alberg
Member
 
Registered: Jun 2016
Distribution: Arch, Slackware
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
if fasd works as intended, it achieves the same some other way.
maybe ncurses?
Well fasd worked as intended, but doesn't work anymore after a recent system update (I'm on rolling-release Arch). This question is about what has caused the change.

fasd doesn't use ncurses, you can take a look at it through the link in one of above posts.
 
Old 01-03-2017, 06:39 AM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by alberg View Post
Well fasd worked as intended, but doesn't work anymore after a recent system update (I'm on rolling-release Arch). This question is about what has caused the change.
aah, now we're getting somewhere!
i just installed fast-git and nothing happens when i first
Code:
To use, run the following: eval "$(fasd --init auto)"
and then
Code:
fast -i
:-(

the normal command prompt just returns, as if i entered "true".
 
Old 01-03-2017, 06:50 AM   #12
alberg
Member
 
Registered: Jun 2016
Distribution: Arch, Slackware
Posts: 47

Original Poster
Rep: Reputation: Disabled
ondoho,

cd several times in various directories, with some common part in the name. Then run 'zz <common_part_of_name>' and a fasd menu should appear.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Why doesn't interactive shell send SIGHUP to children of subprocesses on exit? ntubski Programming 6 11-08-2015 11:07 AM
Exit shell script NotionCommotion Linux - Newbie 8 06-23-2013 09:35 AM
Shell Script running in another shell script and then exit dreamervlk Linux - General 3 09-16-2011 06:40 AM
Temporary storage of password in shell script ansbaradigeidfran Programming 9 10-25-2006 09:03 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:38 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