#!/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
#!/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 :(
]]>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.
]]><Ctrl-R>[[ <Escape>v (edit away...) :wq
@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 ~]$
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.
]]>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?
]]>read -ep "Edit and execute: " -i "$(command -v <something>)" COMMAND
eval "$COMMAND"
You can edit commands right in the command line, if you use bash. I think zsh may let you do that too.
]]>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.
]]>If you source the file where you have your prompt defined, that should do the trick.
]]>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