Hot answers tagged

14

Use -R flag: -r or --raw-control-chars Causes "raw" control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (octal 001) is displayed as "^A". Warning: when the -r option is used, less cannot keep track of the actual appearance of the screen (since this depends on ...


5

That's not the here string, it's ANSI-C quoting: Words of the form $'string' are treated specially. ... The expanded result is single-quoted, as if the dollar sign had not been present. So what you've got is a single-quoted string to the right of <<<. That string gets taken as the here string, with no further processing. There's no need to ...


4

You could use convert from ImageMagick: convert <input_image> -fill '#000' +opaque '#000' <output_image> What it does it changes all the colors not matching #000 to #000.


4

Would not be better if you compress your files and keep less of older logs instead of spending time creating a script? THIS is why logrotate exists. Take a look at your logrotate.conf. It should start with something like this: # see "man logrotate" for details # rotate log files weekly weekly # keep 4 weeks worth of backlogs rotate 4 # create new (empty) ...


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

Use a while loop: while read -r -p "Yes or no? " response && [[ $response =~ ^([yY][eE][sS]|[yY])$ ]] do ... done Or to make that code POSIX sh compatible so you don't need to have bash installed: while printf 'Yes or No? ' && read answer do case $answer in ([yY][eE][sS] | [yY]) ...;; (*) break;; esac done ...


4

OK, i believe i understand the question now. host1 Let us assume that you have host1 to which you connect with ssh via putty, on this host you shall install screen or tmux (from the package manager, they shall be there). I suggest screen since it is a little easier for a beginner. Also, you need to be able to login to host2 from host1 therefore you need ...


3

Try this: #!/bin/bash while :; do ping -c 1 8.8.8.8 >/dev/null 2>&1 if [ $? = 0 ]; then break else echo 'No internet' fi sleep 1 done mpg123 /home/user/file.mp3 It will show you 'no internet' message if there is no ping response. And if it gets the response it will execute your command and quit.


3

Try this script instead: #!/bin/sh sed -i -e "s/processors=[0-9]\+/processors=$(nproc)/g" ~/Scripts/test.sh sed is a Stream EDitor. -i switch tells it to make all changes inplace, -e though optional, tells that the following argument is a sed script


3

So many options, pick the one you like. Using grep: grep -o '^[^:]\+:[^:]\+' file.txt using cut: cut -d: -f1-2 file.txt using awk: awk -F: '{ print $1$2 }' file.txt using sed: sed 's/^\([^:]\+:[^:]\+\).*/\1/' file.txt using shell: while IFS=: read -r i j k; do echo "$i$j"; done <file.txt using perl: perl -pe 's/^([^:]+:[^:]+).*/$1/' ...


3

The [ ... ] syntax is actually a command, not anything special to the shell at all. It is another name for the test command—see man test or even just man [. What you want is arithmetic evaluation: delete="$((size-value))" What is happening in your code is that since you didn't quote the value you are assigning to the variable, that whole line was ...


3

That's 4 uses of host and one of whois. The only way you could speed that up would be to run the commands as background jobs and arrange to check for their completion. That would be a rewrite, which the question declines. To make a background job of each, you'd do something like this, redirecting output to temporary files: ( host -t mx $inp ...


3

You need to keep asking for a response until it isn't one you want: while true; do read -r -p "Yes or no? " response if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]] then echo "You chose yes" else exit 0 fi done


3

This is a terminal independent way of enabling the blinking attribute. If it doesn't work then either you've mis-set your terminal type, it's not enabled in the terminal characteristics, or it's simply not supported: tput blink echo hello, world tput sgr0 The terminfo database is well worth perusing (not bedtime reading, mind) to find semi-readable names ...


3

for i in {15360..15871}; do printf "2607:f4a0:3:0:250:56ff:feac:%.4x\n" $i; done Output: 2607:f4a0:3:0:250:56ff:feac:3c00 2607:f4a0:3:0:250:56ff:feac:3c01 2607:f4a0:3:0:250:56ff:feac:3c02 2607:f4a0:3:0:250:56ff:feac:3c03 . . . 2607:f4a0:3:0:250:56ff:feac:3dfd 2607:f4a0:3:0:250:56ff:feac:3dfe 2607:f4a0:3:0:250:56ff:feac:3dff


3

Using sed with a string You have correctly identified the problem: $reg is a string, not a file. Thus, you need to supply the string to sed as stdin. Replace: reg1=$(sed /^Domain/d $reg) with (for bash): reg1=$(sed /^Domain/d <<<"$reg") Or, for a general POSIX shell, use: reg1=$(echo "$reg" | sed /^Domain/d) Simplification: combining the ...


3

These are all various forms of parameter expansion with alternatives: ${var:-val} is replaced by val if var is unset or null, ${var} otherwise (so val is a "default value"); ${var:=val} first assigns val to var if var is unset or null, and then (in all cases) is replaced by ${var}; ${var:+val} is replaced with nothing if var is unset or null, val ...


2

If anyone want to hack on this some more, here are a messy though perhaps some hints or helps that catch interest. The procedure requests max height and width from file names to generate labels that fits. One feature missing is the vertical centering of the text (which, TBH, looks better.) To get width + height for a text (as integer) one way is to do: ...


2

I'm no ImageMagick expert, so there is probably a better way to do this, but you can do this in 2 steps, first adding the text to the left of each image into an intermediate file, then doing the montage. for file in foo*.png do convert "$file" -splice 100x0 -gravity west -annotate +10+0 '%f' /tmp/"$(basename "$file")" done montage -mode concatenate -tile ...


2

Well, for one thing, you are running three separate external programs when one would be enough. You could do all the parsing and arithmetic in awk for example: xrandr --output LVDS-1 --brightness $(xrandr --prop --verbose | awk "/Brightness:/{print \$2 $1 0.1; exit}") The exit in the awk script ensures that it doesn't need to parse the whole output ...


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

Use <<-EOF instead of <<EOF when you want to use indentation. Then indent the text by tabs. Finally you need to put EOF to mark the end of the here-document. Your code block will be like: ftp -inv $HOST <<-EOF user $USER $PASSWORD cd /work/test//$input_variable/path/to destination/ mput x.csv ...


2

A "CLI tool that can follow the output of that specific file over some period of time" would be tail --follow=name --retry filename. It will print lines of the file as they are added, and will read from the start of the file if it gets removed and re-created, but unfortunately it may miss lines if the file gets truncated. Test 1: Terminal 1 (notice how it ...


2

The seeming challenges of the question / request is perhaps the recursion aspect. Assuming that cmp is an adequate utility and that both folder / directories 1 & 2 to be compared are of the same structure (ie same files & folders) and reside within the same root path - you can try something similar to: #!/bin/bash ROOT=$PWD ; # #// change to ...


2

Here's a fairly simple and straight-forward shell script that uses jsonpipe to do what you want. It doesn't use any fancy sh/bash features, and does only the bare minimum sanity checking of filenames. NOTE: jq is far more capable than jsonpipe, but jsonpipe is simpler and easier to use when you don't particularly care (or want to know) about the structure ...


2

If files are exactly the same, then their md5sums will be exactly the same, so you can use: find A/ B/ -type f -exec md5sum {} + | sort | uniq -w32 -D An md5sum is always exactly 128 bits (or 16 bytes or 32 hex digits) long, and the md5sum program output uses hex digits. So we use the -w32 option on the uniq command to compare only the first 32 ...


2

Use the -v flag: reg=`whois stackoverflow.com | egrep -i 'Registrar|Sponsoring Registrar|Registrant' | grep -v internic`


2

As it is speed what you are looking for: We can do one host call with -t ANY instead of the four used now hoping to get all the four resolutions in one. That will need parsing of the answer. If the whois call could be started and while waiting for an answer from the whois servers we can make the host calls to the DNS servers, we can get the fastest ...


2

scp provides file copy over an ssh connection, so the ability of ssh to automatically execute a command upon connection is what scp uses to copy the file(s), so you can't tap on that. You have two options: 1) copy the file via ssh from a linux/cygwin host: cat local-file | ssh host 'cat > remote-file & your-command' 2) run a cron job to monitor a ...


2

I'm (brand) new to zsh, so this could probably be improved: tile_lengths=() num=19 times=6 repeat $times { tile_lengths+=( $num ); ((--times)) } num=18 times=9 repeat $times { tile_lengths+=( $num ); ((--times)) } print $tile_lengths 19 19 19 19 19 19 18 18 18 18 18 18 18 18 18 Inspired by terdon's answer, I think this is a cleaner variation of my ...



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