Hot answers tagged

16

It's not really a question of ordering checks, simply the order in which the shell sets things up. Redirections are set up before the command is run; so in your example, the shell tries to open ~root/log for appending before trying to do anything involving ./write_file.py. Since the log file can't be opened, the redirection fails and the shell stops ...


14

Using exec makes the wrapper more transparent, i.e. it makes it less likely that the user or application that calls the script needs to be aware that it's a relay that in turns launches the “real” program. In particular, if the caller wants to kill the program, they'll just kill the process they just launched. If the wrapper script runs a child process, the ...


10

From the bash man page, section REDIRECTION (emphasis by me): Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. ... A failure to open or create a file causes the redirection to fail. So the shell tries to open the target file for stdout, which fails, and the command isn't ...


8

You don't need Shift + k. Using Esc + v will work since you are allowing shell command line editing using the built-in vi editor using set -o vi (same can be acheieved with Ctrl + x + e). This is equivalent to execute the builtin fc command which is useful to manipulate the history list and history file. It will invoke whatever editor is set in your $EDITOR ...


7

This allows you to construct a command with full Vi editing. If you type some commands in and save exit :wq the commands will be run. CLARIFICATION: it allows you to construct the command in whatever editor you have set in $EDITOR and when you save and quit from it the contents will be run. (Clarified that it's not just Vi!) ALSO, as noted by RealSkeptic, ...


5

Eric Blake answered on the bash-bugs mailing list: jobs is an interesting builtin - the set of jobs in a parent shell is DIFFERENT than the set of jobs in a subshell. Bash normally creates a subshell in order to do a pipeline, and since there are no jobs in that subshell, the hidden execution of jobs has nothing to report. Bash has code to ...


5

IFS="\n" # Handle files with spaces in the names for file in ABS*; do newfile="${file/ABS/ABS-}" # Add the hyphen following ABS newfile="${newfile/_/-}" # Change the underscore to a hyphen mv "$file" "$newfile" # Make the change done In light of Tony's comments below, a more general use version could be as ...


5

In: echo $IFS you did not double quote variable, the variable content under affect of glob+split operator in all Bourne-like shells, except zsh: echo glob(split($IFS)) The characters in IFS itself are used for splitting, so $IFS expanded to nothing, you only got an empty line from echo. When you double quote "$IFS", the content of IFS was passed to ...


5

Your error message already gives a hint as to what's wrong! Your one-liner is providing the actual file content as filename to the qrencode program. Hence the error message. Try qrencode -o test.png -t png < private.key. You should take a look at shell input-output redirection. For example, I/O Redirection. I see that you too have found your way to ...


5

The $(date +%F --date "Yesterday") technically isn't a variable, it's a command substitution, but that is tangential to your question. This construct could prove to be problematic if for some reason the date command wasn't in your $PATH, and thusreturned nothing - at which point it would delete everything in /video/. If you instead take that command ...


4

Finding no duplicates... refer to the FreeBSD handbook, which gives a good enough reason: The exec statement replaces the shell process with the specified program. If exec is omitted, the shell process remains in memory while the program is executing, and needlessly consumes system resources. which is essentially the reason explained to me quite a ...


4

That's right. > truncates the file before the command is started, so the command sees an empty input file. It doesn't actually matter that the redirections are performed from left to right (except that you'll get an error if the file doesn't exist, whereas >file <file would create the file first). With somecommand <file >>file, most of the ...


4

ShellCheck is a good start for bash programming. It gives quite useful hints: Line 6: if [[ "$CDTRACK" =~ "([[:alpha:][:blank:]]*)- ([[:digit:]]*) - (.*)$" ]] ^-- SC2076: Don't quote rhs of =~, it'll match literally rather than as a regex. Regex can't be quoted like this. Working example with escaped special characters (basically ...


3

This isn't via bash, but the GNOME Shell extension Auto Move Windows lets you define rules so that programs are automatically opened on predefined workspaces. If that doesn't do you what you need, you could consider extending the extension (source here; it's in Javascript) to better fit your specific need.


3

Bash can be invoked in various ways, including directly using its executable (/bin/bash) or a link to it; this is often the case with /bin/sh. "Otherwise" covers this case, and the filename used to invoke Bash is just that — bash, sh, /bin/sh etc. Try it and you'll see: bash then echo $0 prints bash, /bin/bash then echo $0 prints /bin/bash, etc. Arguably ...


3

I just found out that this is not possible. % wc -c ~/private.key 6709 /home/toogley/private.key (-c counts characters.) to cite from wikipedia: max characters for alphanumerical characters: 4,296.


3

The >> /tmp/output already sends all output to the file, leaving nothing to be sent to tee. So the command should read node test.js 2>&1 | tee --append /tmp/output.


3

Newlines are handled specially by bash, regardless of the value of IFS. A backslash before a newline causes the newline to be ignored. Has nothing to do with word splitting and would occur even if IFS were set to some custom value. From LESS=+/^QUOTING man bash: A non-quoted backslash (\) is the escape character. It preserves the literal value ...


3

For python at least I recomend "Learn Python The Hard way", by Zed Shaw. Freely available online. Good stuff. Not sure if posting a link here is technically advertising... Here goes. Free Book


3

To avoid running one dig and read per line of the file, you could do: dig -f domains.txt mx +noall +answer Which would give an output like: stackexchange.com. 300 IN MX 5 alt1.aspmx.l.google.com. stackexchange.com. 300 IN MX 1 aspmx.l.google.com. stackexchange.com. 300 IN MX 10 ...


3

A suitable answer depends on how the names might vary. You could transform the names using the shell's built-in parameter substitution if you assume constant field-widths. That's relatively limited in scope. More interesting would be using character classes in sed: newname=$(echo "$oldname" | sed -e 's/^\([[:alpha:]]\+\)\([[:digit:]]\+\)_/\1-\2-/') ...


3

Quoting the ArchLinux wiki: Note: Because of the process substitution, you cannot run this command with sudo - you will need a root shell. You should be able to use su -c under sudo like so: $ sudo su -c 'wpa_supplicant -D nl80211,wext -i wlp4s0 -c \ <(wpa_passphrase "some ssid" "password")'


3

Process substitution <(…) creates a pipe, uses /dev/fd to give a path that's equivalent to the file descriptor where the pipe is, and passes the file name as an argument to the program. Here the program is sudo, and it passes that argument (which is just a string, as far as it's concerned) to wpa_supplicant, which treats it as a file name. The problem is ...


3

First of all, A single quote may not occur between single quotes, even when preceded by a backslash., ref Bash Manual Second, you may want to use some other char as separator instead of / as you have / in the replacement string. So as a result: sudo sed "s#listen = 127.0.0.1:9000#listen = '/var/run/php56-fpm.sock'#g" /etc/php-fpm.d/www.conf don't ...


3

If you're using -print0 you should use the -0 flag to xargs so it will read the names correctly. find ~/.jenkins/jobs/subco/workspace/myproject/ -name 'target' -print0 | xargs -0 rm -rf alternately, if you have GNU find you could use the -delete flag, though it won't work if the directories are not empty find ~/.jenkins/jobs/subco/workspace/myproject/ ...


3

Let's measure your tests to see how many instructions are done in each method. I created my own file seq 2000000 > 2000000.txt and I want to find which method is the fastest. $ perf stat sed -ne '10p' 2000000.txt 10 Performance counter stats for 'sed -ne 10p 2000000.txt': 203.877247 task-clock # 0.991 CPUs utilized ...


2

Firefox puts itself in the background and detaches from your terminal if you run it from a terminal. Therefore pressing Ctrl+Z is not really suspending Firefox, it probably suspends xinit. wget and most commandline programs do not have this kind of detaching behaviour. GUI based programs like gedit start mostly like firefox and detach. I looked if there ...


2

try using other chars rather than / for separation maybe? sudo sed -i "s@listen = 127.0.0.1:9000@listen = '/var/run/php56-fpm.sock'@g" /etc/php-fpm.d/www.conf OR sudo sed -i "s/listen = 127.0.0.1:9000/listen = '\/var\/run\/php56-fpm.sock'/g" /etc/php-fpm.d/www.conf The problem is that you are not escaping the / as \/ but using @ as separator will fix ...


2

Answer here: What does !#:3 mean in a shell command Basically, you can use it to shorten a command in combination with ':n', so: $ cd /home/me/some/super/deep/dir/that/i/do/not/want/to/type/again ; ll !#:2 Of course this is a bit silly example, because you could just do ll, but you get the idea, it can be used in sh scripts.


2

Yes, It's possible [[ "a" == "a" ]] && echo true || echo false Try replacing "a" with "b", to see false: [[ "b" == "a" ]] && echo true || echo false



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