Tag Info

Hot answers tagged

13

If the command is not very picky it should work with something like this: command > /dev/null << EOF <answer 1> <answer 2> <answer 3> EOF This requires that you know the exact answers beforehand.


12

Oooh, I found an explanation. To quote the relevant part: The zsh shell comes with (more than one) great feature(s), such as remote tabcompletion. If you for example want to copy a file over scp, simply hit tab at any part of the filename on the remote host. zsh is able to establish an ssh session on the background, and fetch the related information ...


12

Expect can do that. From the Expect website: Expect is a tool for automating interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc. Expect really makes this stuff trivial. Expect is also useful for testing these same applications [...]" It comes with a lot of help, like autoexpect. Again from the Expect website, autoexpect ...


9

The line which ends the here document is \$fff From the man bash section on Here Documents: The format of here-documents is: <<[-]word here-document delimiter No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any ...


8

Not a neat answer but an alternative if you're using bash as your shell: you could createt some alias in your .bashrc. For instance: alias a='cd /tmp/A ; history -w ; history -c ; export HISTFILE=/home/user/.a_history ; history -r $HISTFILE' alias b='cd /tmp/B ; history -w ; history -c ; export HISTFILE=/home/user/.b_history ; history -r $HISTFILE' Then, ...


7

That's newline from echo, you can verify by using echo -n to suppress trailing newline: echo -n "$(ls | grep png)" Command substitution remove all trailing newlines, last newline was added by echo, grep has nothing to do here.


7

From the manual (man bash): $(command)  or  `command` Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) ...


6

Solution Using Parallel You could use GNU Parallel for a compact, faster solution. find . -type d -print0 | parallel -0 cd {}'&&' <command-name> This will work absolutely fine, even for directory names containing spaces and newlines. What parallel does here is that it takes the output from find, which is every directory and then feeds it to ...


6

The nullglob option (which BTW is a zsh invention, only added years later to bash (2.0)) would not be ideal in a number of cases. And ls is a good example: ls *.txt Or its more correct equivalent: ls -- *.txt With nullglob on would run ls with no argument which is treated as ls -- . (list the current directory) if no files match, which is probably ...


5

The find command is powerful, but that makes it a little challenging to use. I'm pretty sure it can do what you need - this command below is "almost" what you ask for: find . -type d -execdir pwd \; But - this does not run the command in the deepest directory level - it runs in the directories in which other directories are found. So it wil run in ...


5

I'd use Perl's paragraph mode: pactl list sink-inputs | perl -00ne 'print if s/(.*?VLC.*?\n).*/$1/ms' The -00 sets the input record separator to \n\n so a "line" is a paragraph. Then, the substitution will match everything until the first VLC and then anything until the 1st newline and save them as $1. Everything after that is removed (since we're ...


5

With ed: ed -s <<'IN' r !pactl list sink-inputs /VLC/+,$d ?Sink Input?,.p q IN It reads the command output into the text buffer, deletes everything after the first line matching VLC and then prints from the previous line matching Sink Input up to current line. With sed: pactl list sink-inputs | sed -n 'H;/Sink Input/h;/VLC/{x;p;q}' It appends ...


5

If your script expects one prompt answered, or several prompts in which you can give the same answer, there's yes: NAME yes - output a string repeatedly until killed SYNOPSIS yes [STRING]... yes OPTION DESCRIPTION Repeatedly output a line with all specified STRING(s), or `y'. Use it like this: yes Me | give_a_hug.sh


4

No need for bash here, any standard sh interpreter implementation will do: #! /bin/sh - ret=0 for file do dir=$(dirname -- "$file") case $dir in (*[!/]*) dir=$dir/ # handle / and // specially esac base=$(basename -- "$file") name=${base%.*} name=${name:-$base} # don't consider .bashrc the extension in /foo/.bashrc ext=${base#"$name"} ...


4

[ $i = "*f*" ] splits the file name stored in the variable i into separate words at spaces, interprets each word as a wildcard pattern and expands it if it matches, and then parses the resulting list of words as a condition for [ … ]. To avoid this rigmarole and instead use the file name, put double quotes around the variable expansion. [ "$i" = "*f*" ] ...


4

sed -i '$a gem '"'"'forum2discourse'"'" Gemfile Alternate Solution If you wish to do it your way, then use the bash $'string' format. Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. sed -i $'$a gem \'forum2discourse\'' Gemfile Source: ...


4

You can just use grep as a pager: man -P 'grep NR' awk but it is way better to just search for pattern with / in less (that is probably your default pager), so: man awk and then /^ *NR This way you will find only headers (patterns at the beginning of the lines).


4

The terminator is \$fff I'm going to assume that that's what @rici meant to say.  As his penultimate paragraph says, word does undergo quote removal, so \\$fff is dequoted to \$fff. But, as the man page says, no variable expansion is done so it stays that way.


4

#!/bin/sh - urldecode() { python -c "import sys, urllib as ul;print ul.unquote_plus(sys.argv[1])" "$1" } str="this+is+%2F+%2B+%2C+.+url+%23%24coded" decoded=$(urldecode "$str"} printf '%s\n' "$decoded" That is: avoid aliases in scripts as that's not guaranteed to work (some sh implementations like bash ignore aliases when non-interactive) quote your ...


3

With the command you tried, echo is printing every word in the manual page on a single line. You would have had a better luck with: echo "`man awk`" | grep NR or better echo "$(man awk)" | grep NR or even better, given the fact echo is useless here: man awk | grep NR Note that most if not all man implementations detect their output is a pipe and ...


3

Assuming you are using bash (or zsh) and your mail agent is sendmail then: [[ -f "filename_$(date '+%m%d%Y')" ]] || echo "File is missing!" | sendmail myname@gmail.com Between [[ and ]] we test if file exists, and if not then print some message and send it to myname.


3

The quick answer is checksum="${checksum% || })" instead of checksum+=")". Just unconditionally add the || string in each step and then strip off the last unnecessary one at the very end (so the line_number computation is no longer needed). A better way to do this is awk -F, 'BEGIN { printf "md5( " } toupper($2) != "DATE" { printf "%s%s", sep, ...


3

The problem lies in how you're calling the . special builtin: exec /bin/sh -c '. vars.sh; /usr/bin/fish' In sh, if the argument doesn't contain any /, . searches for the file in $PATH. So above, it would look for vars.sh in $PATH instead of the current directory as you intended. Also, . being a special builtin, its failure causes the shell to exit (when ...


3

Capturing exit status of commands The assignment of command output to the rep variable does not lose the exit status of the curl command; it is still available as $?. For more details, see How can I store the return value and/or output of a command in a variable?. Curl exit code for failed HTTP requests Usually if a requested HTTP resource isn’t ...


3

With zsh, you could do: mkdir -p ~/.zsh/dirhist And add to your ~/.zshrc: HISTSIZE=1000 SAVEHIST=10000 setopt HIST_SAVE_NO_DUPS INC_APPEND_HISTORY HISTFILE=~/.zsh/dirhist/${PWD//\//@} chpwd() { [[ $PWD = $OLDPWD ]] || fc -Pp ~/.zsh/dirhist/${PWD//\//@} } chpwd() is called whenever the current directory changes. There, we reset the history file to ...


3

From the wikipedia's "Checksum" article: A checksum or hash sum is a small-size datum from a block of digital data for the purpose of detecting errors which may have been introduced during its transmission or storage. It is usually applied to an installation file after it is received from the download server. By themselves checksums are often used to ...


2

local stty="$(stty -g)" Save the current terminal settings. stty $stty, which is executed both when the function returns normally and on SIGINT, restores these settings. trap "stty $stty; trap SIGINT; return 128" SIGINT If the function is interrupted by SIGINT (the signal sent by pressing Ctrl+C), restore the terminal settings and return 128. (Why ...


2

I have found the solution first enable on sshd_config user environement PermitUserEnvironment yes Then edit $HOME/.ssh/environement and put PATH and whatever you want The $HOME/.ssh/environment file can't contain other than variables and comments for example good environment HOME=/home/user PATH=$PATH:/opt/freeware/sbin:/bin:/opt/freeware/bin bad ...


2

These are effectively multi-line records separated by a blank line. Awk is great for handling this kind of data: pactl list sink-inputs | awk -v RS="" '/VLC/' If you want to be really nit-picky about not including the bottom part of the record after the first occurrence of "VLC", then: pactl list sink-inputs | awk -v RS="" -v FS="\n" '/VLC/{ for(i=1; ...


2

In fish, you can use funcsave to save function definition across fish session: $ function qwerty echo qwerty end $ funcsave qwerty $ fish -c qwerty qwerty $ perl -e 'system "fish -c qwerty"' qwerty



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