Tag Info

Hot answers tagged

12

Never embed {} in the shell code! (by the way, some find implementations won't let you do that, and POSIX leaves the behaviour unspecified when {} is not on its own in an argument to find) find . -name accept_ra -exec sh -c 'echo 0 > "$1"' sh {} \; Or: find . -name accept_ra -exec sh -c 'for i do echo 0 > "$i"; done' sh {} + Note that the ...


11

You can just use grep: grep -Fwf fileA fileB From man grep: -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) -f FILE, --file=FILE Obtain patterns from FILE, one per line. The empty file ...


9

man bash says: ... redirection operators may precede or appear anywhere within a simple command or may follow a command. while is not a simple command.


7

Always try and break things down into there simplest steps, then try and put things together after. To start the first part, I'd construct myself a sample file. $ echo -e "line 1 ABA\nline 2 ABB\nline 3 CCC\n" > xxx $ cat xxx line 1 ABA line 2 ABB line 3 CCC So now we have file xxx. Now we need to use something that can act as program yyy. Unix is ...


5

You can in zsh, not in bash and choroba has already pointed you to the documentation, but if you want to have the redirection before, you can do things like: < file eval ' while IFS= read -r line; do ... done' Or (on systems with support for /dev/fd/n): < file 3<< 'EOF' . /dev/fd/3 while IFS= read -r line; do ... done EOF (not ...


5

Have you tried to use aliases? example Create an alias for the new command: alias ntpversion='ntpq -c "rv 0 version"' Run the command: ntpversion Running ntpversion, after it is set as an alias, will provide the output for your one-liner. http://www.linuxhowtos.org/Tips%20and%20Tricks/command_aliases.htm


4

Use process substitution: diff <(cat /etc/passwd) <(cut -f2 /etc/passwd) <(...) is called process substitution. It converts the output of a command into a file-like object that diff can read from.


4

I think you could do something as suggested here. (ls -l | echo "Hello" | df -h & echo $! >&3 ) 3>pid Here in the above example, I have retrieved the pid of third piped process and noted it down to the file pid. I could note it down for any piped process.


4

For the original version of the question, when only the last command's PID was desired, the special variable $! is perfect. foo | bar | baz & baz_pid=$! There's no similar easy access to the PIDs of the other processes. It took a long time for $pipestatus (zsh) and $PIPESTATUS (bash) to be added, finally giving us access to all of the exit statuses ...


4

Try each command - you'll be surprised what you'll learn. Read man top - you'll be surprised what lurks within man pages. Use Google - you'll be surprised what's available on the Internet. See '3'.


3

One robust way in bash is to expand into an array, and output the first element only: pattern="*.txt" files=( $pattern ) echo "${files[0]}" # printf is safer! This safely handles space/tab/newline and other metacharacters when expanding the filenames. You can also do this interactively with a bash completion function: _echo() { local ...


3

You can use that substitution, if you want to precede the input: cat lines | while read line; do echo "line: $line"; done


3

Run the script either as: bash script.sh or just: ./script.sh When bash is run using the name sh, it disables most of its extensions, such as the [[ testing operator. Since you have the #!/bin/bash shebang line, you don't need to specify the shell interpreter explicitly on the command line. Running the script as a command will use that line to find ...


3

Does the order of fileA matter? Can you have multiple lines in fileB with that pattern? This will for example parse fileA and search for each pattern in fileB: while read i; do grep "$i" fileB; done < fileA But you need to define the problem better to get a solution with more performance. For example it is sufficient to get the whole line, you don't ...


2

You can use exec to redirect the stdin. In a script: exec < <(cat lines) while read line ; do echo "line: $line"; done You can't use in a login shell though (it will dump the file on the stdout and exit). In that case you can open a different file descriptor: exec 3< <(cat lines) while read -u 3 line ; do echo "line: $line"; done For ...


2

There's a little utility called rmtrash which does this. It doesn't seem to respond to params like -r or -f (it appears to essentially just be moving the file/directory to the ~/.Trash directory), but it won't override files with the same name (it appends "Copy" to like-named files/directories). To install with brew brew install rmtrash alias ...


2

Running commands from vim You asked about vim. In vim, you can create a file with your shortcuts. It would have lines such as: !ntpq -c "rv 0 version" You can then copy that line to the vim command line (:) to execute it. Because the line starts with !, vim will give it the the shell to execute when you press enter. You never have to leave vim. With ...


2

You could make your history very large and then execute stuff from there. And there's commandlinefu.com... You could note all those one liners in a plain text file you keep around. I run a private habari instance where I put this kind of stuff (and more documentation)...


2

In zsh, use the [1] glob qualifier. Note that even though this special case returns at most one match, it's still a list, and globs are not expanded in contexts that expect a single word such as assignments (array assignments aside). echo *.txt([1]) In ksh or bash, you can stuff the whole list of matches in an array and use the first element. tmp=(*.txt) ...


1

This one worked for me: find "$@" ! -type d -exec kill -9 $$ \; -quit && rm -R "$@" If find exits normally (nothing is found) rm -R "$@" will be executed. If find finds something the current shell/script is killed ($$ stores the pid). The rm-part will never be executed in this case.


1

A simple solution: sh -c 'echo "$1"' sh *.txt Or use printf if you prefer.


1

Try: for i in *.txt; do printf '%s\n' "$i"; break; done 1.txt A note that filename expansion is sorted according to the collating sequence in effect in the current locale.


1

A not-very-portable, Linux-specific solution could be to track the processes using the pipes that connect them. We can get the PIDs of the first (jobs -p) and last ($!) commands in the pipeline. Using either PID, this script could do the job: #! /bin/bash PROC=$1 echo $PROC if [[ $(readlink /proc/$PROC/fd/1) =~ ^pipe: ]] then # Assuming first process ...


1

The "|" is a pipe command. Some programs take their input from a pipe, such as "less" or "more". If you run the following command: $ ls -l | less The output of "ls" will be piped through the pager, "less". If you want to run one command then the other from the same line without the pipe you would either use ";" or "&&" (which is probably what ...


1

part of fuser is sent to stdout (standard output), and part to standard error. how is output split ? piping mechanism only catch stdout. plain fuser mybox $ fuser / /: 350r 356r 357r 364r 10484rc 10485r now redirecting, see pid are in a, while type of file (c or r) is in stderr. mybox $ fuser / > a /: ...


1

If you want to pass a pipeline like git … | tar … (it's a single command which contains two subcommands, not two separate commands) directly as an argument to a function, you'll need build a string containing that command, and use the eval builtin in the function to execute this string as a shell command. Take care of proper quoting. For the argument, ...


1

Add alias ll='ls -lG' to your ~/.profile with your favorite $EDITOR. With this method, remember that you'll have to start a new terminal session (or source ~/.profile to be able to use ll).


1

I have similar script written for my personal use. There is a very easy trick to achieve change working directory inside a script. First just write your script, in.ex.: #!/bin/bash case $1 in project1) cd /home/me/work/customer1/project1 ;; project2) cd /home/me/work/customer2/project1 ;; project3) cd ...



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