You are not logged in.

#1 2014-03-04 15:30:05

Mox
Member
Registered: 2012-02-07
Posts: 32

[SOLVED] Bash: Append file contents to command prompt

Hi,

is there a way to append the contents of a file to the command prompt? Say I have defined an alias and I want to change it on the fly. So I'd do

$ which <my_alias>

then mark it with the mouse, and paste it to the command line. What I would like to do instead, would be:

$which <my_alias> > some_temp_file
$vim some_temp_file #edit the command the way I want it
$ # somehow get the file contents into the prompt here

I did try to google it, but to no avail and I know, I could just create a script from some_temp_file but I want the content to appear at the command prompt instead. So is there a way?

regards,
mox

Last edited by Mox (2014-03-07 15:46:09)

Offline

#2 2014-03-04 15:38:04

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Bash: Append file contents to command prompt

I have no idea what do you want to do.
Can you explain it more + why would you do that / that way?

If you source the file where you have your prompt defined, that should do the trick.

Last edited by karol (2014-03-04 15:39:22)

Offline

#3 2014-03-04 15:51:54

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,447
Website

Re: [SOLVED] Bash: Append file contents to command prompt

Karol, I don't think the OP wants it in the *prompt* (though that's how I read the title), I think the OP wants the contents appended to the command line (e.g. pasted, ready for execution).

This isn't a full solution as I don't use which with aliases (and I can't seem to get it to work with aliases ... are you using bash's which, or which which?)  But if you use vi(m)-mode for readline, then after writing to the tempfile you can do <esc> <v> to edit the command line in vim, and you could define a vim macro to read in the temp file ( :r /path/to/tmpfile )

EDIT: you may also consider xsel if this is in X.  Rather than piping to a temp file, you can pipe to xsel, then paste directly onto the command line or into vim.

Last edited by Trilby (2014-03-04 15:58:23)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2014-03-04 15:54:18

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Bash: Append file contents to command prompt

You can periodically check if some_temp_file is empty, and paste the contents if it's not.


You can edit commands right in the command line, if you use bash. I think zsh may let you do that too.

Last edited by karol (2014-03-04 15:55:29)

Offline

#5 2014-03-04 16:11:52

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: [SOLVED] Bash: Append file contents to command prompt

In bash you can do samething like this:

read -ep "Edit and execute: " -i "$(command -v <something>)" COMMAND
eval "$COMMAND"

Last edited by progandy (2014-03-04 16:13:04)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#6 2014-03-05 04:48:34

Mox
Member
Registered: 2012-02-07
Posts: 32

Re: [SOLVED] Bash: Append file contents to command prompt

Hi, thanks for all the answers.

Trilby is right, I don't want it within the prompt, but where commands are entered.
@Karol: Of course I know, I can edit commands in the command line (and I use vi-mode and <esc>v ) but that will only edit commands I have already entered into the command line.
The use case I described is different: Say I have defined this alias:

$ which tce
tce: aliased to [[ -e /dev/sdb3 ]] && truecrypt -k "" --fs-options=users,uid=$(id -u),gid=$(id -g),fmask=0113,dmask=0000 --mount /dev/sdb3 /mnt/tce

Which works when sdb3 is formated to ntfs, but now I want to run the same command without the fs-options part. I cannot just edit some command in the command line, because it just is not there. So this is the real challenge: Get this command into the command line like this

$ [[ -e /dev/sdb3 ]] && truecrypt -k ""  --mount /dev/sdb3 /mnt/tce

without using the mouse. And, if possible without doind periodic checking for file contents or so. I think this is just as inelegant as using the mouse.

@progandy: Yes, this might be a step in the right direction. But what do the -i and -v options do? I did not find any documentation in the manpage. And is there a way to expand $COMMAND in the command line before running $COMMAND?

Offline

#7 2014-03-05 05:00:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,447
Website

Re: [SOLVED] Bash: Append file contents to command prompt

Mox wrote:

But what do the -i and -v options do? I did not find any documentation in the manpage.

POSIX's read does not have those flags, but bash's builtin does, so check out the bash man page (line 29.27).

Pipe the result of `which` into xsel, then on the command line type Shift-Insert.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2014-03-05 05:05:45

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: [SOLVED] Bash: Append file contents to command prompt

Mox wrote:

@progandy: Yes, this might be a step in the right direction. But what do the -i and -v options do? I did not find any documentation in the manpage. And is there a way to expand $COMMAND in the command line before running $COMMAND?

Why do you want to have the command in a prompt twice? "read -er COMMAND && eval "$COMMAND"" creates a prompt-like environment and then executes the input string. What the options do you can read in the bash-internal help, e.g.:

$ LC_ALL=C help read
...
      -e		use Readline to obtain the line in an interactive shell
      -i text	Use TEXT as the initial text for Readline
...
      -p prompt	output the string PROMPT without a trailing newline before
    		attempting to read
      -r		do not allow backslashes to escape any characters
...

Here is an example:

[progandy@pamobile ~]$ alias some_test='echo -e Hello WORLD"!"'
[progandy@pamobile ~]$ command_to_edit="some_test"
[progandy@pamobile ~]$ read -erp "Edit and execute: " -i "$(command -v $command_to_edit)" COMMAND
Edit and execute: echo -e Hello WORLD"\t!"
[progandy@pamobile ~]$ ####### in the line above you edit the command and bring it in the final form with all necessary paramters.
[progandy@pamobile ~]$ echo -E " *~* Now running: $COMMAND"
 *~* Now running: echo -e Hello WORLD"\t!"
[progandy@pamobile ~]$ eval "$COMMAND"
Hello WORLD	!
[progandy@pamobile ~]$ 

Last edited by progandy (2014-03-05 05:21:39)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#9 2014-03-05 05:41:19

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] Bash: Append file contents to command prompt

Why don't you just retrieve the command from your shell history and then edit it from there?

<Ctrl-R>[[ <Escape>v (edit away...) :wq

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#10 2014-03-05 05:51:56

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: [SOLVED] Bash: Append file contents to command prompt

I forgot that there is a simple keybinding to expand an alias: enter the alias and press ctrl-alt-e


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#11 2014-03-07 15:45:00

Mox
Member
Registered: 2012-02-07
Posts: 32

Re: [SOLVED] Bash: Append file contents to command prompt

@progandy:
Well no, I did not mean to have the command in the command prompt twice. I just wanted to be able to change it before it is executed. So your example answers my question. Thank you very much!
The keybinding to expand aliases does not work for me, but I guess, I will have to search my bashrc, maybe something prevents it usage there. If I get this to work, it will be an even better solution for this use case, I think.

So I think this thread should be closed. Thank You all.

p.S.> I don't want to search the command from history, because I restrict my history to the current session so there is a good chance the command will not be in the history.

Offline

#12 2014-03-07 16:48:01

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] Bash: Append file contents to command prompt

See the bind built-in in the Bash manual. You can use it to list existing readline functions and their mappings, and also to set them up.
"alias-expand-line" and "shell-expand-line" are (I think) the two you might be interested in.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#13 2014-07-14 04:25:13

мимо проходил
Member
Registered: 2014-07-14
Posts: 1

Re: [SOLVED] Bash: Append file contents to command prompt

I use the X-only-terminal-script

#!/bin/bash
inp=`cat`;
read cmd < <(tr '\n' ' ' <<< $inp);
xdotool type --clearmodifiers --delay 0 "$cmd";
xdotool key --delay 0 ctrl+l

The last line is awful, but an output of the script without it is more awful :(

Offline

#14 2014-07-15 05:52:57

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [SOLVED] Bash: Append file contents to command prompt

Instead of an alias, just use a simple bash script like the below which uses findmnt to determine the filesystem type, then executes the appropriate command based on that type.

#!/usr/bin/bash

partition="/dev/${1:-sdb3}"
[[ -e "$partition" ]] || exit

fstype=$(findmnt -no FSTYPE "$partition")
declare -A opts=([ntfs]=" --fs-options=users,uid=$(id -u),gid=$(id -g),fmask=0113,dmask=0000")
printf -v cmd 'truecrypt -k ""%s --mount %s /mnt/tce' "${opts[$fstype]}" "$partition"
$cmd

If you want to use this on a partition other than sdb3, you can pass the partition name as a parameter e.g.:

$ tce sdb4

Offline

Board footer

Powered by FluxBB