Tag Info

Hot answers tagged

16

You can simulate a CTRL-Z (which you normally use to temporarily background a process) using the kill command: [tsa20@xxx01:/home/tsa20/software]$ kill -19 $$ [1]+ Stopped sudo -iu tsa20 [root@xxx01 ~]# fg sudo -iu tsa20 [tsa20@xxx01:/home/tsa20/software]$ bash just traps the CTRL-Z key combination. kill -19 sends SIGSTP to the process ...


11

To be able to time a subshell, you need the time keyword, not command. The time keyword, part of the language, is only recognised as such when entered literally. Even entering "time" won't work let alone $TIME (and would be taken as a call to the time command instead). You could use aliases here which are expanded before another round of parsing is ...


11

It's a matter of timing: bash launches the hello command in the background, then it displays a prompt to let you enter a new command, then the background command prints some output. When you enter the next command line (an empty command line, if you just press Enter), bash displays the notification that the background job has finished, then the next prompt. ...


7

rm [0-9][0-9].* will do it for files in the current directory (no quotes — you want to match files). The . doesn't need to be escaped, because this is a shell glob and not a regular expression (if it were a regex, that would be a wildcard). If you are looking to do this recursively, find is probably your best bet.


6

Assuming that there's at least one space before the substring you wish to extract (and that the substring does not contain any spaces), you can do this with a simple parameter expansion: x="rtcpOnNbActive true" y="${x##* }" echo "[$y]" output [true]


6

you can use the --attributes-only switch of cp for this purpose, eg. find . -iname "*.txt" -exec cp --attributes-only -t dummy/ {} + From the man page of cp: --attributes-only don't copy the file data, just the attributes This will create empty files with all attributes of the original file preserved but no contents.


5

shift is a bash built-in which kind of rotates the arguments. Given that the arguments provided to the script are 3 available in $1, $2, $3, then a call to shift will make $2 the new $1. a shift 2 will shift by two makeing new $1 the old $3. for more info see here http://ss64.com/bash/shift.html http://ss64.com/bash/shift.html ...


5

Recursively : find . -type f -name '[0-9][0-9].*' -delete require GNU find, or : find . -type f -name '[0-9][0-9].*' -exec rm {} \;


5

> bash -c 'exec -a sadhadxk sleep 1000000' & pgrep doesn't work but > ps | grep '[s]adhadxk' 18931 [...] sadhadxk 1000000 Correction: pgrep does work but not against the command name (which is the name of the running binary), only against the command line: > pgrep -f sadhadxk 18931


4

To reassure a few, I didn't find the bug by observing exploits, I have no reason to believe it's been exploited before being disclosed (though of course I can't rule it out). I did not find it by looking at bash's code either. I can't say I remember exactly my train of thoughts at the time. That more or less came from some reflection on some behaviours of ...


4

Yes, that's a race condition. The problem is that the shell starts all processes in the pipeline at the same time and tee truncates the output file on startup. If tee is faster then comm the file is empty for comm otherwise it is not. The pipeline behaviour can be seen if you run this several times (mabe in a loop): date '+first: ...


3

While an alias is one way to do it, this can be done with eval as well - it's just that you don't so much want to eval the command execution as you want to eval the command declaration. I like aliases - I use 'em all the time, but I like functions better - especially their ability to handle parameters and that they needn't necessarily be expanded in command ...


3

Each part of pipelines run in separated processes, or own subshell. So when your pipelines finished, your current shell does not know anything about function f. With bash (ksh, pdksh, zsh, mksh or shell that support Here-String), your can use: $ source /dev/stdin <<<'f() { echo a; }' $ f a POSIXly, you should use Here-Document and dot: $ . ...


3

You could do: perl -e '$0="sadhadxk"; sleep infinity' & It should set both the process name and argv[0] on systems where it's supported so should show sadhadxk in both ps and ps -f output, so should be matched by both pgrep -x and pgrep -fx.


3

If the trailing space is not included in HISTTIMEFORMAT, then you won't have a space between the timestamp and the command. Here are some examples: HISTTIMEFORMAT="%F %T: " This results in: 33916 2014-12-18 11:03:08: echo foo Without the space: HISTTIMEFORMAT="%F %T:" 33916 2014-12-18 11:04:11:echo foo


2

You can use ps+grep or pgrep to get the process name/pid; later use killall/pkill to kill process name or use kill to kill pid. All of the followings should work. killall $(ps aux | grep script_name | grep -v grep | awk '{ print $1 }') && killall inotifywait (ps -ef | grep script_name | grep -v grep | awk '{ print $1 }' | xargs killall) && ...


2

The command you have above will (somewhat clumsily) rename all files in the current directly from *.jpg to *.jpeg, it could be modified to delete all files but is hardly appropriate to the task. However, it sounds like you are trying to craft a suitable filename such that when the above command encounters it, it will delete everything in the current ...


2

The commands in a pipe are separate processes, hence the function definition that is sourced from /dev/stdin is lost as soon as the pipe completes. That is why the pipe show different results to the usage of the temporary file. In your use case the eval as suggested by PM 2Ring would be the way to go.


2

Better use an XML parser with a XPath expression to modify the XML file. One example with xmlstarlet : $ xmlstarlet edit -L -u "/connection/output[contains(., "Arturia")]" -v "remplacement_string" file.xml


2

Without recreating the subdirectories: find . -type f -name '*.jpg' -printf /path/to/emptys/%f\\0 | xargs -0 touch


2

With current bash version: #!/bin/bash declare -A numbers # declare associative array printf -v numbers[2004] "%s " {625..721} printf -v numbers[2005] "%s " {723..823} for year in 2004 2005 do for number in ${numbers[$year]} do echo "$year $number" done done


2

As goldilocks’ comment and humanity’s references describe, shift reassigns the positional parameters ($1, $2, etc.) so that $1 takes on the old value of $2, $2 takes on the value of $3, etc.*  The old value of $1 is discarded.  ($0 is not changed.)  Some reasons for doing this include: It lets you access the tenth argument (if there is one) more easily.  ...


2

Use an array since that can expand to a variable number of arguments: #!/bin/bash # This is file caller.bash switch=() if [[ ${1-x} == x ]] then switch=("--abc=long argument") fi some_command.sh "--exclude=*~" "${switch[@]}" arg Or you could use the ${var+...} syntax: #!/bin/sh # This is file caller.sh unset switch if [ "${1-x}" = x ] then ...


2

You can do something like: func > >(tee log.txt) 2>&1 wait You can dedicate a file descriptor for logging: exec 3> >(tee log.txt) tee_pid=$! func >&3 2>&1 ... Beware though that as that tee runs in background, if not all the output goes through it, then the order in the output may be affected.


2

It's nothing to do with SSH. The -x argument to bash is that of bash's set command, which displays the command's arguments in expanded form. This is why the double quoted strings are displayed as single quoted strings. $ cat test.sh echo "here are 'some single quotes' inside double quotes" $ bash -x test.sh + echo 'here are '\''some single quotes'\'' ...


2

There is no method specific to PHP when it comes to running jobs in the background. The widely accepted methods are: Using a screen. Install screen (if it is not already installed), then run your PHP script under a screen. You can detach from the screen anytime and log out of the machine and the script will continue running. Here is one of the few hundred ...


2

Checking for existence will reduce the problem, but in the most general case it's a race condition. The file could still be removed between the check and the copy attempt. Perhaps just capture all errors and drop any for "file doesn't exist". Normal copy: $ cp noexist bar /tmp cp: cannot stat `noexist': No such file or directory cp: cannot open `bar' for ...


1

Try doing this : sleep 600 &


1

Here's a variant on Cyrus's answer that uses parameter indirection. However, as the link says, array-based approaches are to be preferred over the use of indirection, as such indirection is a close cousin of eval, which should be avoided whenever possible. (I've reduced the ranges of the numbers from those given in the OP just to make the output a bit ...


1

You can use process substitution source /dev/stdin < <(echo -ne 'f() { echo a; }\n') or source <(echo -ne 'f() { echo a; }\n') This works in bash 4.1.5, for some reason it doesn't work in 3.2.48.



Only top voted, non community-wiki answers of a minimum length are eligible