Wrap command in linux

How do I get long command lines to wrap to the next line?

Something I have noticed in Ubuntu for a long time that has been frustrating to me is when I am typing a command at the command line that gets longer (wider) than the terminal width, instead of wrapping to a new line, it goes back to column 1 on the same line and starts over-writing the beginning of my command line. (It doesn’t actually overwrite the actual command, but visually, it is overwriting the text that was displayed). It’s hard to explain without seeing it, but let’s say my terminal was 20 characters wide (Mine is more like 120 characters — but for the sake of an example), and I want to echo the English alphabet. What I type is this:

echo abcdefghijklmnopqrstuvwxyz 
abcdefghijklmnopqrstuvwxyz 

so I know the command was received properly. It just wrapped my typing after the «o» and started over on the same line. What I would expect to happen, if I typed this command in on a terminal that was only 20 characters wide would be this:

echo abcdefghijklmno pqrstuvwxyz 

to be able to navigate the command line with VI commands. I am currently using Ubuntu 10.10 server, and connecting to the server with Putty. In any other environment I have worked in, if I type a long command line, it will add a new line underneath the line I am working on when my command gets longer than the terminal width and when I keep typing I can see my command on 2 different lines. But for as long as I can remember using Ubuntu, my long commands only occupy 1 line. This also happens when I am going back to previous commands in the history (I hit Esc, then ‘K’ to go back to previous commands) — when I get to a previous command that was longer than the terminal width, the command line gets mangled and I cannot tell where I am at in the command. The only work-around I have found to see the entire long command is to hit «Esc-V», which opens up the current command in a VI editor. I don’t think I have anything odd in my .bashrc file. I commented out the «set -o vi» line, and I still had the problem. I downloaded a fresh copy of Putty and didn’t make any changes to the configuration — I just typed in my host name to connect, and I still have the problem, so I don’t think it’s anything with Putty (unless I need to make some config changes) Has anyone else had this problem, and can anyone think of how to fix it? Edit It was my .bashrc file. I’ve copied the same profile from machine to machine, and I used special characters in my $PS1 that are somehow throwing it off. I’m now sticking with the standard bash variables for my $PS1. Thanks to @ændrük for the tip on the .bashrc! . End Edit.

Читайте также:  Что такое линукс rpm

Источник

Wrap all commands entered within a Bash-Shell with a Python script

What i’d like to have is a mechanism that all commands i enter on a Bash-Terminal are wrapped by a Python-script. The Python-script executes the entered command, but it adds some additional magic (for example setting «dynamic» environment variables). Is that possible somehow? I’m running Ubuntu and Debian Squeezy. Additional explanation: I have a property-file which changes dynamically (some scripts do alter it at any time). I need the properties from that file as environment variables in all my shell scripts. Of course i could parse the property-file somehow from shell, but i prefer using an object-oriented style for that (especially for writing), as it can be done with Python (and ConfigObject). Therefore i want to wrap all my scripts with that Python script (without having to modify the scripts themselves) which handles these properties down to all Shell-scripts. This is my current use case, but i can imagine that i’ll find additional cases to which i can extend my wrapper later on.

Could you please elaborate more what you want to do? How are you going to wrap shell commands and how are you going to execute them? It can done in two (or maybe more ways): a) handle the command as a string, add something to that string and run that string in bash; b) get the command, execute the bash using subprocess or something else and pass the command (preceded by some another command).

Источник

Wrapping a command that includes single and double quotes for another command

I recently learned about watch, but am having trouble making it work with relatively sophisticated commands. For example, I would like to ask watch to run the following command on zsh every three seconds * :

for x in `command_1 | grep keyword | cut -d' ' -f1`; do command_2 "word[word=number]" $x; done 

as you can see the line above includes single quotes, double quotes, among other special characters. So I tried:

watch -n 3 "for x in `my_command | grep keyword | cut -d' ' -f1`; do command2 "rusage[mem=7000]" $x; done" 
watch -n 3 "for x in $(bjobs -w | grep pre_seg | cut -d' ' -f1); do bmod -R "rusage[mem=7000]" $x; done" 

which also results in a similar error. Any ideas how to make this work? * I would also be intersted in solutions that work on bash

Читайте также:  Обеспечение локальной безопасности linux

1 Answer 1

General tip: if you have two levels of nesting, avoid using single quotes in the inner command, and use single quotes around the outer command.

Additional tip: don’t use backticks — `…` — to execute code, instead use $(…) around it. Dollar-parentheses is pretty much DWIM(‘Do what I mean’) when it comes to nested quotes; backquotes have arcane, shell-dependent rules.

watch -n 3 'for x in $(my_command | grep keyword | cut -d" " -f1); do command2 "rusage[mem=7000]" "$x"; done' 

If you need single quotes inside a single-quoted command, you can use ‘\» . Think of these four characters as the way to quote a single quote inside single quotes, though technically speaking this is constructed as end the single-quoted string, append a literal single quote, and start a new single-quoted string (still appended to the current word).

For more complex cases, either painstakingly count the quotes, or define temporary variables.

cmd='for x in $(my_command | grep keyword | cut -d" " -f1); do command2 "rusage[mem=7000]" "$x"; done' watch_cmd='watch -n 3 "$cmd"' 

This answer isn’t specific to zsh. Zsh doesn’t bring anything major here. You can save a bit of quoting because there’s no need for double quotes around command substitutions, and sometimes there are ways to use built-ins rather than external commands which reduce the quoting needs, but the underlying issues are the same as in other shells.

Oh, and by the way, note that watch will execute your command in sh , not in zsh. If you want to execute the command in zsh, you need to run

export cmd watch -n 3 'exec zsh -c "$cmd"' 

(even more quoting!) elsewhere.

Источник

Bash (or other shell): wrap all commands with function/script

Edit: This question was originally bash specific. I’d still rather have a bash solution, but if there’s a good way to do this in another shell then that would be useful to know as well! Okay, top level description of the problem. I would like to be able to add a hook to bash such that, when a user enters, for example $cat foo | sort -n | less , this is intercepted and translated into wrapper ‘cat foo | sort -n | less’ . I’ve seen ways to run commands before and after each command (using DEBUG traps or PROMPT_COMMAND or similar), but nothing about how to intercept each command and allow it to be handled by another process. Is there a way to do this? For an explanation of why I’d like to do this, in case people have other suggestions of ways to approach it: Tools like script let you log everything you do in a terminal to a log (as, to an extent, does bash history). However, they don’t do it very well — script mixes input with output into one big string and gets confused with applications such as vi which take over the screen, history only gives you the raw commands being typed in, and neither of them work well if you have commands being entered into multiple terminals at the same time. What I would like to do is capture much richer information — as an example, the command, the time it executed, the time it completed, the exit status, the first few lines of stdin and stdout. I’d also prefer to send this to a listening daemon somewhere which could happily multiplex multiple terminals. The easy way to do this is to pass the command to another program which can exec a shell to handle the command as a subprocess whilst getting handles to stdin, stdout, exit status etc. One could write a shell to do this, but you’d lose much of the functionality already in bash, which would be annoying. The motivation for this comes from trying to make sense of exploratory data analysis like procedures after the fact. With richer information like this, it would be possible to generate decent reporting on what happened, squashing multiple invocations of one command into one where the first few gave non-zero exits, asking where files came from by searching for everything that touched the file, etc etc.

Читайте также:  Linux запрос пароля при входе

This won’t be possible without modifying bash itself. Is that within your range of possibilities? Even then, it might prove difficult. Your concept of «command» might not correspond to any internal bash structure. See the odd example in this answer: stackoverflow.com/questions/20039771/…

Instead of write a shell yourself or modify bash, maybe open to the other shells out there (i think especially of zsh and maybe fish).

This sounds similar to what servers do with requests. For example, Apache does it with varying levels of verbosity, and there are a bunch of modules in Node, Ruby, and other languages. Perhaps you could make a request on every command? Kind of a lot of work and it’ll balloon your Internet bill, but if there’s no better way.

Источник

Оцените статью
Adblock
detector