New answers tagged

1

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


0

On Mac OS X, the only thing I can get to work is man -k . | grep -F '(3)', which lists everything in section 3.


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


1

Your problem is that you are trying to set a variable inside a here-doc: ftp -inv $HOST <<- EOF user $USER $PASSWORD mypath='/test/$input_variable/destination/' if ! cd "$mypath" then exit 1 fi mput x.csv EOF That can not work. Change that part to this: mypath="/test/$input_variable/destination/" ftp -inv $HOST ...


-1

You can create a script.sh that contains all your commands, each ended by &. A bash script will execute one by one the commands listed. #! /bin/bash command1 &; command2 &; However if your commands are not bash commands but scripts or other programs, bash will play the role of a launcher. Therefore it will concider that his work is done ...


3

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


0

I have also experienced some issues with adding executables to my path. What worked for me is adding the line export PATH=$PATH:/home/ahuq/MappingServer/md5_program to the ~/.bash_profile. Unfortunately I don't know why but for met .bashrc didn't work but .bash_profile did.


0

Change your single quotes to double quotes. Instead of: mypath='/test/$input_variable/destination/' Use: mypath="/test/$input_variable/destination/"


1

Your main problem is that you think you're setting a variable with your line mypath='/test/$input_variable/destination/' but it is actually run inside the FTP session. You need to move it above the FTP command. You also check conditions after it that can not be checked there for the same reason.


0

You could start by not instantiating Python on every matching byte (!): #!/usr/bin/awk -f function de() { getline < "/proc/uptime" close("/proc/uptime") return $0 } BEGIN { ec = de() } $0 == "ed" { getline byte1 getline byte2 number = strtonum("0x" substr(byte2, 2, 1)) + 2 data = ":: " de() - ec " ::ED." byte1 "." byte2 while (number--) ...


0

I was skeptical of Stéphane’s answer, however it is possible to abuse $#: $ set `seq 101` $ IFS=0 $ [ $# = 0 ] bash: [: too many arguments This is a contrived example, but the potential does exist.


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


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


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


0

If this script is ran as root...then removing the entry from crontab can be done by removing its crontab line from /var/spool/cron/root. (This is what crontab -l and crontab -e use). At the end of your delete commands add the following line (substituting {pattern} for your script name): sed -i '/{pattern}/d' /var/spool/cron/root So if your entry in ...


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


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


0

You should use a better value for pi and allow the scale to be as is defined by default in bc (20 digits): $ echo "r=3; pi=4*a(1); (4/3)*pi*(r^3)" | bc -l 113.09733552923255658339 That is a better way to perform the calculation. Scale is a parameter to define the precision used in the calculation, not a parameter to format the numeric output of the ...


1

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


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


0

Very Fast Way to Kill Multiple Processes (without having to write a script) USAGE: ./autokill.kl.sh <proc-name> => This will give you 5 seconds to change your mind and control-C out of the program. USAGE: ./autokill.kl.sh <proc-name> now => This will immediately kill all processes matching the string/pattern you specified!!! MODEL: ...


2

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


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()' ...


0

sed solution: $ str='Hellowww.hello.comMywww.world.comWorld' $ echo "$str" | sed -e 's/com/com\n/g' | sed -ne '/.*www\.\(.*\)\.com.*/{ s//\1/p }' hello world


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

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


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


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


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


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


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


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


0

You can use, sar -w. For instance, sar -w 1 3, reports total number of context switches per second for every 1 seconds a total of 3 times.


-1

If you want it to apply in all accounts, you can also put alias ll='ls -lG' in /etc/profile.


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

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

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

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.


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


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

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.


0

This had me thinking. The problem with sed/awk/tail is that they're line by line. After you delete the first line you have to write every other line from pattern space to the file. You can use the following commands to do what you want in seconds. This will write the entire file to an array. Remove the first line as it dumps it back into the file. ...


0

Bringing back a file to its orginal location is not different from moving a file to that location. In Linux you do that with the mv command: mv /path/to/output/location/your_file.ext /path/to/original/location You can of course include this line (at the end of) any script that creates the file at the output location.


0

Much thanks to @stéphane-chazelas who pointed out all the problems with my previous attempts, this now seems to work to serialise an array to stdout or into a variable. This technique does not shell-parse the input (unlike declare -a/declare -p) and so is safe against malicious insertion of metacharacters in the serialised text. Note: newlines are not ...


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


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


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


0

A solution - not one that is as elegant as those that change the *RS variables, but perhaps reasonably clear: PATH=`awk 'BEGIN {np="";split(ENVIRON["PATH"],p,":"); for(x=0;x<length(p);x++) { pe=p[x]; if(e[pe] != "") continue; e[pe] = pe; if(np != "") np=np ":"; np=np pe}} END { print np }' /dev/null` The entire program works in the BEGIN and END ...


0

Try this command: rm -rf $(find . -name thatcertainfile -execdir pwd \;) It should say to the rm -rf that what it had to remove is the output of your command. For example, if your command's output was /home/guest/Documents the command I showed would translate on rm -rf /home/guest/Documents.


2

It's worth observing that the shell must establish redirections before starting the program. Consider your example: ./write_file.py >> ~root/log What happens in the shell is: We (the shell) fork(); the child process inherits the open file descriptors from its parent (the shell). In the child process, we fopen() (the expansion of) "~root/log", and ...



Top 50 recent answers are included