Tag Info

New answers tagged

2

The following function permits to change to sibling directories (bash function) function sib() { ## sib search sibling directories ## prompt for choice (when two or more directories are found, current dir is removed from choices) ## change to directory after selection local substr=$1 local curdir=$(pwd) local choices=$(find ...


2

You can also handle ps output a little better. ps --width ${n:-$COLUMNS} ${opts} #set ps terminal width ps -ww ${opts} #no word wrap ps -o ${only_interesting_output} ${opts} #trim output That will tell ps to parse its output to your specifications as necessary. Of course, if you don't word wrap, though, then you've got the problem of missing info. Do ...


1

There is a very long-standing convention (in Unix) for return-status ranges: 0 means success positive numbers mean minor problems, but essentially the task completed negative numbers mean critical error (e.g. disk-full, file-not-found) Exactly what those individual positive and negative numbers mean is up to the programmer. Sometimes you can choose them ...


3

Some shells like zsh, bash or mksh automatically set the $COLUMNS variable to the width of the terminal, so you don't need to invoke stty here. All the implementations of ps I tried that support that non-standard (BSD-type) syntax query the terminal width by themselves. I'm surprised yours doesn't. I expect it will look at the content of the COLUMNS ...


2

The following works: ps aux | cut -c1-$(stty size </dev/tty | cut -d' ' -f2) This also works: v=$(stty size | cut -d' ' -f2) ; ps aux | cut -c1-$v The problem seems to be that stty needs to have the tty on its standard input in order to function. The above two approaches solve that. There is still another option. While stty's stdin and stdout are ...


1

In general this information cannot be obtained. print.sh can easily determine that its input is coming from a pipe, but not what is at the other end of the pipe. The first process may even have terminated, with its output fully contained in the pipe buffer. In this case, not even traversing the process list will give you any information. Whatever it is ...


0

Normally, if you finish your script at some point with simply: exit The shell will get a 0 as the return code. This zero means everything was OK. You can of course set it explicitly: exit 0 However, if your program has found some error condition, you should exit with a non-zero return code, to inform the shell that something has gone wrong. If you ...


0

An answer for most modern linux-OS systems. the file /etc/os-release is really becoming standard and is getting included in most OS. so inside your bash script your can just include the file, and you will have access to all variables described here (eg: NAME, VERSION, ...) so I'm just using this in my bash script: if [ -f /etc/os-release ] then . ...


0

Some programmers will supply a lot of different errorcodes starting with 1. New versions might introduce new specific errorcodes, so which code should be used for "all other errors / unspecified error" ? The exit codes will be truncated at 255, so I would choose 99 as an "other error".


11

In addition to @Chris Down, there is some return code that reserved for the shell, they have special meaning: RETVAL Meaning 1 General errors 2 Misusage 127 Command not found You can refer to this for more details.


21

There is no significance to exiting with code 99, other than there is perhaps in the context of a specific program. Either way, exit exits the shell with a certain exit code, in this case, 99. You can find more information in help exit: exit: exit [n] Exit the shell. Exits the shell with a status of N. If N is omitted, the exit status is that ...


0

In zsh: als() { until [[ $1 = [/.] ]] {argv[1,0]=$1:h;}; ls -ld -- "$@" } POSIXly: als() ( while :; do case $1 in [./]) exec ls -ld -- "$@" esac set -- "$(dirname -- "$1")" "$@" done )


0

I think this is POSIX portable: . <<ENV /dev/stdin $(sed -n 'H;${x;s/\(^\|\x00\)\([^=]*.\)\([^\x00]*\)/\2\x27\3\x27\n/gp}' \ /proc/$pid/environ) ENV But @Gilles makes a good point - sed will probably handle nulls, but maybe not. So there's this (I Really think so this time) actually POSIX portable method, too: s=$$SED$$ sed ...


6

In this answer, I assume a system where /proc/$pid/environ returns the environment of the process with the specified PID, with null bytes between variable definitions. (So Linux, Cygwin or Solaris (?)). Zsh export "${(@ps:\000:)$(</proc/$pid/environ)}" (Pretty simple as zsh goes: an input redirection with no command <FILE is equivalent to cat FILE. ...


4

In bash you can do the following. This will work for all possible contents of the variables and avoids eval: while IFS= read -rd '' var; do declare +x "$var"; done </proc/$PID/environ This will declare the read variables as shell variables in the running shell. To export the variables into the running shell environment instead: while IFS= read -rd '' ...


3

The easiest way to link to the current directory as an absolute path, without typing the whole path string would be ln -s "$(pwd)/foo" ~/bin/foo_link The target argument for the ln -s command works relative to the symbolic link's location, not your current directory. It helps to imagine that the created symlink simply holds the text you provide for the ...


0

How about: $ cd /Users/niels/something $ ln -s ./foo ~/bin/foo_link


10

The following will convert each environment variable into an export statement, properly quoted for reading into a shell (because LS_COLORS, for example, is likely to have semicolons in it), then sources it. [The printf in /usr/bin, unfortunately, generally doesn't support %q, so we need to call the one built into bash.] . <(xargs -0 bash -c 'printf ...


2

Using source and process substitution: source <(sed -r -e 's/([^\x00]*)\x00/export \1\n/g' /proc/1/environ) Shortly: . <(sed -r -e 's/([^\x00]*)\x00/export \1\n/g' /proc/1/environ) Using eval and command substitution: eval `sed -r -e 's/([^\x00]*)\x00/export \1\n/g' /proc/1/environ` The sed call can be replaced with an awk call: awk ...


-2

eval \`(cat /proc/1/environ; echo) | tr '\000' '\n'\`


1

I'm surprised other answers have not mentioned TERMINFO (or TERMCAP) Use the man pages Luke man clear says ... NAME clear - clear the terminal screen SYNOPSIS clear DESCRIPTION clear clears your screen if this is possible. It looks in the environ- ment for the terminal type and then in the terminfo database to figure ...


7

How about (cd lib && echo *.jar), assuming that you don't have whitespace or special characters in the file names. Parent script never changes directory.


3

I am not entirely clear on how you get the output you show. I am assuming it is produced by the script you mentioned and that you can simply pipe it through something else to parse it. If so, these solutions should work: your_script | tail -n 2 | awk '/RMS/{print $4}' tail -n 2 prints the last two lines and the awk will print the 4th field of any line ...


2

you could use awk (revised based on comments): awk '{a2 = a1; a1 = $4;} END { print a2 }' then just pipe your outpot into awk and you'll get 7.457 According to PyMOL's website, it's highly configurable and scriptable. It would be better to post the script you are using to SO and get some Python feedback.


3

script | sed -n '${x;p};h' That should do it, I think. It will always print the second to last line. If you want only the number you: script | sed -n '${x;s/[^0-9]*\([^ ]*\).*/\1/p};h' So big H appends to sed's hold space the contents of the current pattern space, whereas little h overwrites it. So if you overwrite the hold space for every line, and on ...


-1

An alternative way solve your query is to list all the files using ls -R. Combine the output of ls command with grep to list only .jar files. You can use following command to do the same for your query: ls -R lib | grep jar| grep -v jar*


5

With GNU find there is no need to run basename for every single file, this will be much faster (especially if there is a lot of files): find lib -name '*.jar' -printf '%P\n'


0

find is probably the way to go, but if you really, really do (you don't) want to strip off lib/ from ls -1, you can use sed: $ ls -1 lib/*.jar | sed 's#^lib/##' mylib_1.jar mylib_2.jar


6

As Josh Jolly said in his answer, you should never parse ls, use the approach in his answer instead. Still, here's an awk solution to remove paths from file names, just don't use it with ls: find . | awk -F'/' '{print $NF}' The -F'/' sets the field separator to / which means that the last field, $NF, will be the file name.


14

Instead of parsing ls you should use find instead. Then you can also execute basename on each file to strip the leading directories: find lib/ -name '*.jar' -exec basename {} \;


6

Many of the compressors take an an environment variable to accept options that cannot be passed on the command line. In your case GZIP_OPT=-9 sort --compress-program=/bin/gzip The same is true for xz with XZ_OPT and bzip2 with BZIP2


2

Linux (and no acl support): namei -l /foo/bar/baz


1

The problem is you want php to read input from a file descriptor, but you force it to read like regular file. First, try this: $ echo <(ls) /dev/fd/63 then you can process ls output by reading /dev/fd/63. The process substitution will return the file descriptor, which is use by other command to reading its output. In your case, you use ...


1

This application looks like what you're looking for. It's call gsutil. This pages discusses how to install gsutil, a tool that enables you to access Google Cloud Storage from the command-line. gsutil runs on Linux/Unix, Mac OS, and Windows. To use gsutil, you must have Python 2.6.x or 2.7.x installed on your computer. gsutil does not currently run ...


2

I can't think of any expansion trick or utility to do it all in one go. So a loop is the way to go. Here's some code that works under both bash and zsh, and accommodates directories with arbitrary names. ## Usage: set_directory_chain VAR FILENAME ## Set VAR to the chain of directories leading to FILENAME ## e.g. set_directory_chain a /usr/bin/env is ...


0

sh <<-STRESS & $( printf 'myprog &\n%.0b' \ `seq 1 ${MAX_CONCURRENT_PROCS}` ) STRESS echo "$!" I agree with the comment @msw makes above. This will write you a script to be launched by a backgrounded sh process and print out the child sh process's pid so you can monitor it and its children as it works.


3

How about using brace expansions? $ ls -ld /{,usr/{,bin/{,tee}}} drwxr-xr-x 23 root root 4096 Mar 7 06:57 / drwxr-xr-x 10 root root 4096 Jan 9 2013 /usr/ drwxr-xr-x 2 root root 40960 Apr 9 23:57 /usr/bin/ -rwxr-xr-x 1 root root 26176 Nov 19 2012 /usr/bin/tee


0

tmux has this capability. (along with many other useful capabilities in the same vein)


7

This is simply done with an alias; alias ls="ls -1" You can put this in your .bashrc file, although it probably already contains the following alias to give colourised output: alias ls="ls --color=auto" In which case you would just add to it giving: alias ls="ls --color=auto -1"


4

In addition to all nice answer above, we can do some strace to see what happen: $ strace -e trace=write echo -e "\x1b\x5b\x48\x1b\x5b\x32\x4a\c" write(1, "\33[H\33[2J", 7 $ strace -e trace=write clear write(1, "\33[H\33[2J", 7 You can see, two command provide the same ANSI escape sequences.


8

The output sent by clear(1) depends on your terminal type, defined by $TERM in the shell environment. It does the same thing as the command "tput clear", which is looking up the escape code for the current terminal type and sending that string to standard output. The terminal receiving the escape code from clear/tput interprets it and executes the command ...


18

The output of the clear command is console escape codes. The exact codes required depend on the exact terminal you are using, however most use ANSI control sequences. Here is a good link explaining the various codes - http://www.termsys.demon.co.uk/vtansi.htm. The relevant snippets are: Cursor Home <ESC>[{ROW};{COLUMN}H Sets the cursor ...


14

It works by issuing certain ANSI escape sequences. Specifically, these two: Esc[Line;ColumnH          Cursor Position: Esc[Line;Columnf            Moves the cursor to the specified position (coordinates). If you do not ...


2

Screen is a bit heavy handed. A second way is to use the old school method of nohup. nohup script command 2>&1 > /dev/tty1 & The nohup command captures all hangup signals and ignores them, so the the command left after will not receive and there for not stop on closing your terminal.


2

Spawn your script within a screen session. Redirect output to TTY as you proposed. Detach from the screen session and close the terminal. No SIGHUP will be sent so the script should continue to run.


3

ls -ld `echo 'path/to/file' | sed ':0 p;s!/[^/]*$!!;t0' | sort -u` sed part: :0 label 0; p print; s!p!r! replace pattern p with replacement r; /[^/]*$ search for /, then any sequence of not-/ till the end of line; replacement is empty, so just delete the match; t0 if s!!! performs a replacement, then go to label 0. Edit by OP after comments I did the ...


11

Without quotes the string is subject to word splitting and globbing. See also BashPitfalls #14. Compare $ echo $(printf 'foo\nbar\nquux\n*') foo bar quux ssh-13yzvBMwVYgn ssh-3JIxkphQ07Ei ssh-6YC5dbnk1wOc with $ echo "$(printf 'foo\nbar\nquux\n*')" foo bar quux * When word splitting occurs the first character of IFS acts as a separator (which, per ...


4

The likely most important difference would be if the directory that the script is in has a space in it. In that case, the first line, the one without the double-quotes, would fail. This would be the result of "word splitting" which bash does on unquoted strings. Suppose that the result of dirname ${BASH_SOURCE[0]} is /home/j r/bin. Consider the line ...


6

The main difference is that the quoted version is not subject to field splitting by the shell. With double quotes the outcome of the command expansion would be fed as one parameter to the source command. Without quotes it would be broken up into multiple parameters, depending on the value of IFS which contains space, TAB and newline by default. If the ...


2

Use backslashes to protect $ and " inside the remote command: ssh host "netstat -rn|awk 'NR!=1 && NF>=6 && \$1!=\"Destination\" {printf \"%-15s %-20s\n\", \$1, \$2}'|sort -f "



Top 50 recent answers are included