Hot answers tagged

60

Because $ is a special character to the shell you should put the password in between single quotes: useradd -p '$6$Ic2PVlwi$2nf.IRWTMy0FHrPza6mh5wjomwbYtIIxnzxPZL7yg8SsvOdbjEpoI0G8uy7AqduYKQOn2R/rnnaalRmfPMy.a0' bwong20 without these the shell will try to expand $6, $Ic2PVlwi and $2 to their respective variable values, and as the variables are not set, ...


13

The simplest solution is to just not use extensions for your scripts. They are not necessary and only serve to identify the script's type to you, but not to the computer. While Windows uses extensions to identify the file type, *nix systems (with very few exceptions such as gzip) do not. Note that binaries have no .exe extension in *nix, they're just ...


13

You need to understand the meaning of the scale of an expression in bc. bc can do arbitrary precision (which doesn't necessarily mean infinite precision) while your calculator will probably have the precision of the float or double data type of your processor. In bc. The scale is the number of decimal after the dot, so related to the precision. The scale of ...


10

Sourcing your script only sets shell variables, while printenv shows environment variables. You will have to export the variables for printenv to show them. You may have meant to use set instead, which will show shell variables. You could have made this script: #!/bin/sh export MYVAR=MYVAL echo "EXECUTED!!" (given that you are using bash, the export ...


10

The variable is available in the main script, since you used . (the dot builtin, also known as source in some shells). . ./vars executes vars in the context of the calling script. Your problem is that you're using printenv to check, but printenv only prints environment variables, not shell variables. Environment variables are managed by the operating system ...


7

If you really want to do it, there is a way. Add the following at the end of .bashrc in your home directory, and set PATHEXT to extension names with dots separated by :. (Changed to include the dots to match the Windows behavior.) Use it at your own risk. if declare -f command_not_found_handle >/dev/null; then eval ...


6

short: no longer: shell scripts require a full filename, but you can define aliases for your commands to refer to them by various names. For example alias my-script=my-script.pl


5

This will give you how many miliseconds have passed since the beginning of the Unix epoch : date +%s%3N You have to reduce the seconds*1000 which have passed until 00:00 AM today. Or you can just convert everything into miliseconds from this formula : date +%H%M%S%3N Like : h=$(date +%H) m=$(date +%M) s=$(date +%S) ms=$(date +%3N) echo ...


4

I am not sure what your intention is (you didn't make that clear), but if it's to chmod to 700 all the files that match the pattern, then, except for your typo (;\ instead of \;), your command seems to work as intended. However: when it finds a file containing that string grep -q gives me 0 so another exec executes but should not. Yes, it should do. 0 ...


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 ...


3

See if perl is portable enough: perl -MTime::HiRes=time -MPOSIX=strftime -e ' $now = int(time() * 1000); printf "%s.%03d\n", strftime("%H:%M:%S", localtime(int($now/1000))), ($now % 1000); ' Time::HiRes and POSIX are both core Perl modules. If you just want the epoch timestamp with fractional seconds: $ perl -MTime::HiRes=time -le 'print time()' ...


3

Use emacs, start an inferior shell and issue your command. The output will be available in the shell buffer and can be selected using the usual commands. Alternatively, select file in $(find <whatever>); do vi $file; break; done The emacs approach is more practical if you already know the editor. Emacs can run arbitrary "inferior processes", ie. ...


3

(command1; command2)& - should do it, works in bash. This creates a subshell (the two parenthesis) and runs the whole subshell in the background.


3

With zsh: printf '%s\n' d/*.txt(:t) :t like for csh history modifiers but here in a glob qualifier, gets you the tail of the file name. Also: files=(d/*.txt) printf '%s\n' $files:t In other Bourne-like shells, you could always do: (cd d && printf '%s\n' *.txt) Note that it doesn't fork a new shell, it creates a subshell environment. In most ...


2

I believe that the simplest way to do what you ask is: $ ( cd d; ls *.txt ) 1.txt 2.txt 3.txt that is happening inside a sub-shell ( ... ) so the directory change is not permanent, is valid only for the execution of the two commands. A more robust version is: $ ( cd d && ls -d -- *.txt ) 1.txt 2.txt 3.txt In which ls is not executed unless ...


2

There's nothing wrong with running vim with sudo. That's the correct way to create a file in a directory that needs root access. As for the rest of the files there being links, again, not a problem. If it makes you feel better, you can create the script in /usr/bin instead but there's absolutely nothing wrong with having a regular file in /usr/local/bin. In ...


2

Your /etc/shells misses newline at the end of file. Edit it with an editor which cares about this, like vi.


2

Given a path to the file ./some/where/thatcertainfile, stripping off the final /thatcertainfile gives you a path to the directory. Launch a shell to be able to use string manipulation on the path. find . -name thatcertainfile -exec sh -c 'rm -r "${0%/*}"' {} \; Alternatively, use zsh. To transform a path into the name of the containing directory, use the ...


2

I use a simple function: mpvs() { local file="$1" mpv --sub-file="${file%.*}".srt "$file" } If you want to test for the presence of subtitle files with different extensions, you could use a more complex approach: #!/usr/bin/env bash # Play subtitles for a film if they exist movie="$1" mdir="${movie%/*}" name="${movie##*/}" cd "$mdir" for file in ...


1

Using the same file for both input and output is guaranteed to give you problems. The problem will start as soon as the shell opens both files because the output file will get truncated. If you append to the output, then it will loop indefinitely until the disk is full or the max file size is reached.


1

It's what it says on the tin: “Maximum length of command we could actually use” is the maximum possible command line length, given the limit on the platform where xargs is running and the space taken up by the environment. This value only depends on the platform configuration and the environment. “Size of command buffer we are actually using” is the size ...


1

According to a comment on Fedora, Adding ~/.local/bin to default PATH, this was a Fedora-specific change in the bash configuration, a few years ago. The change was made in the RPM (not upstream), and undocumented.


1

You can't simply do that because the tee command overwrites the file, making it shorter (probably) and eliminating the cat command's ability to read the data that was in the file. If you could ensure that programs such as tee opened a new file, and if the shell guaranteed that cat opened its copy first, then you could copy from the old (actually deleted) ...


1

The problem is that you cannot guarantee which is executed first. So you have to delay unlinking and writing to the file until you are absolutely sure that the file is opened for reading. This will buffer the file in RAM before writing it. cat foo | perl -e 'undef $/; @out=<>; open WRT,">",shift; print WRT @out' foo Advantage: Keeps ...


1

Solaris isn't fundamentally different from other *nix's. Moreover, looking at bash -t won't give you useful information in all cases. Have you tried last?


1

Turns out that removing all ~/.zcompdump files solved it: rm -r ~/.zcompdump*


1

$ echo "www.blablabla.com" | grep -oP '(?<=\.)[a-zA-Z0-9\.-]*(?=\.)' blablabla -o -- print only matched parts of matching line -P -- Use Perl regex (?<=\.) -- after a literal ., aka, a "positive look-behind" ... [a-zA-Z0-9\.-]* -- match zero or more instances of lower & upper case characters, numbers 0-9, literal . and hyphen ... (?=\.) -- ...


1

With recent Linux, printf foo > /proc/$$/comm will change the executable name (the ps -p thing) provided "noclobber" isn't set (and the wind is in the right direction). In zsh, printf foo >! /proc/$$/comm works regardless of clobbering state.


1

AFAICT from your sample input and output, you want to round the first field to the nearest 10, round down the second field down to the nearest 10, and leave the third field alone. awk '{printf "%5.0f\t%6.0f\t%.2f\n", int($1/10+0.5)*10, int($2/10)*10, $3}' file Output: 50460 170400 0.01 50440 170380 0.03 50460 170380 0.04 50480 170380 0.01 ...


1

You have misunderstood how hard links work. There is no original. All files are simply hardlinks to an inode. Therefore, hardlinks don't actually link to files, they link to inodes. To illustrate, consider this file: $ touch file $ ls -li file 3282140 -rw-r--r-- 1 terdon terdon 0 May 3 16:27 file As you can see above, file points to the inode 3282140. ...



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