How to enter text at command prompt from shell script without executing command?
(Inspired by Abdul Rehman's answer.)
Put this as the last line in your .bashrc
file:
read -e -p "(Control-C to cancel) $ " -i "cd some/dir && ./some_script.sh" && eval "$REPLY"
read -e
lets you enter a string using Readline for editing.- The
-p
provides a pseudo-prompt for this one-time command. - The
-i
provides a default string which you can edit or hit Enter to accept.
If you hit Enter, read
sets the value of REPLY
and exits with status 0, causing the following eval
to execute. (Usually, eval
is not a good command to use; however, here you would be presented with an interactive shell anyway, so there's not much risk here.) If you type Control-C instead, read
exits with status 1 and the following eval
is not executed, leaving you at your command prompt.
How can I run cmd from batch file and add text to line without executing?
Excuse me. I think that the real goal of this question is: "How to prefill the cmd.exe command-line input with an editable string", so the user may just press Enter key to execute the prefilled input, or edit it in any desired way. I copied the method below from this post:
@if (@CodeSection == @Batch) @then
@echo off
rem Enter the prefill value in the keyboard buffer
CScript //nologo //E:JScript "%~F0" "java -jar crawler-1.0.jar"
goto :EOF
@end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
Bash: Set up a new command on a new line without executing
How about
read -e -p"$PWD> " -i"ls -al " cmd; eval "$cmd"
Type a text on shell prompt
bash
doesn't have a way to "preload" the next input buffer (unlike zsh
, in which you can simply use print -z "git checkout $N"
). You would need to have mkbranch
handle the input (and subsequently execute the entered command) itself.
read -p "Enter your branch name: " N
git branch "$N"
read -e -i "git checkout $N" cmd
eval "$cmd"
-e
informs read
to use the Readline library for entering the line of text; the -i
option preloads the input buffer. (Without the -e
the -i
doesn't do anything.)
Note that read -e
does not itself know anything about bash
syntax, so there are no implicit line continuations; this makes it distinctly different from the ordinary bash
prompt.
An alternative would be to simply ask the user if they would like to checkout the newly created branch:
read -p "Enter your branch name: " N
git branch "$N"
read -e -i Y -p "Checkout branch $N? (Y/n)"
[[ $REPLY == Y ]] && git checkout "$N"
Hitting enter accepts the pre-loaded Y to trigger the hard-coded `checkout command; any other command skips it. Either way, the script ends and returns to the regular command prompt.
Command Prompt: Execute Commands from Any Text File (Not Having .bat or .cmd Extensions)
you first need an "installation" script :
@echo off
rem :: A files with .TEST extension will be able to execute batch code but is not perfect as the %0 argument is lost
rem :: "installing" a caller.
if not exist "c:\caller.bat" (
echo @echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\caller.bat
rem :: associating file extension
assoc .test=batps
ftype batps=c:\caller "%%1" %*
then try a simple .test
file:
@echo off
for /l (1;1;10) do (
echo testing .TEST extension
)
In fact ASSOC
and FTYPE
both have immediate effect so you can start a .test
file right after "installation". With direct editing of the registry eventually you can get more control -> How to create file extension that behaves as .cmd/.bat? . Check also drop handlers -> http://msdn.microsoft.com/en-us/library/windows/desktop/cc144165%28v=vs.85%29.aspx
Simple text cleaning python 3.6 script not giving correct output on executing the script using cmd prompt on windows 10
The cmd shell doesn't understand Python's Unicode escape sequences, so you are receiving literal ASCII characters for the escape codes.
If you want to support translating them, you could change you main
call to:
main(args.data.encode(sys.stdin.encoding).decode('unicode-escape'))
And then your output will be:
i'm deciding between Firestik Firefly, 4' 200w, & Firestik FS-3BK, 3' 650w. Is one better? It's for recreational use on
and off road. thank you!
Related Topics
Display Hosts Alive with Fping
Shell Bash Script to Print Numbers in Ascending Order
Make Diff to Ignore Symbolic Link
How to Extract Text Which Matches Particular Fields in Text File Using Linux Commands
Difference Between ./Executable and . Executable
Postgres Copy Command, Binary File
How to Run an Image Windows Docker on Ubuntu
Where Do Char Device Appear After Cdev_Add() Registers Successfully with Major on 117.
Use Stdin from Within R Studio
Starting Youtrack as a Service Fails Without Error Message
Skip Successfully Downloaded Files Using Wget
Can Not Add New User in Docker Container with Mounted /Etc/Passwd and /Etc/Shadow
Communication Between Linked Docker Containers
When Compiling Programs to Run Inside a Vm, What Should March and Mtune Be Set To
Linux: Triggering Shell Command on File Save
Why Sizeof(Spinlock_T) Is Greater Than Zero on Uni-Processor