New answers tagged

1

AWK is part of POSIX, so this should be OK: openssl version | awk '$2 ~ /(^0\.)|(^1\.(0\.|1\.0))/ { exit 1 }' Instead of looking for versions greater than or equal to 1.1.1, this looks for versions less than 1.1.1: versions starting with “0.”, “1.0”, or “1.1.0”.


0

Bit of a bodge, but works: export openssl_minversion=1.1.1 if echo -e "$(openssl version|awk '{print $2}')\n${openssl_minversion}" | sort -V | head -1 | grep -q ^${openssl_minversion}$;then echo okay else echo nokay fi


0

To correct your original solution: Like @Kusalananda said, you should turn "$directory1/*" and "$directory2/*" into "$directory1"/* and "$directory2"/*, respectively or else the for loop will try to look for files literally named "*". If I understand your goal correctly, the line rm "$f2" should be rm "$f1" otherwise the program will just remove the last ...


0

It would be easier with zsh and its ${array1:|array2} array disjunction operator: #! /bin/zsh - dir1=${1:?} dir2=${2:?} dir1_files=(${dir1%/}/*(DN:t)) dir2_files=(${dir2%/}/*(DN:t)) only_in_dir2=(${dir2:|dir1}) (($#only_in_dir2 == 0)) || rm -rf -- $dir2/$^only_in_dir2 With bash and GNU utilities, the equivalent could be something like: #! /bin/bash - ...


1

Here's one way to do it, use find to list files in each directory, use basename and sort to order them. Then feed each list to comm and only print out the files present in the first directory but not the second. Once the list looks the way you expect it to, you can pipe that to rm. comm -2 -3 <(find "$directory1" -maxdepth 1 -type f | xargs -n1 -d\\n ...


0

Copyright James Daniel Marrs Ritchey. This material was created for submission at 'awk - extract value infront of "somestring:"' under the terms of the cc by-sa 4.0 (https://creativecommons.org/licenses/by-sa/4.0/legalcode), but can also be alternatively obtained from 'https://snippetly.blogspot.com/2019/12/extracting-value.html' under the terms of ...


1

I'm not really following this coproc thing. In your case read -p seems just right. And you may want to use a separate (non-default) history file. Warning: the script creates/uses $HOME/.custom_history to save its history. After you test the solution, remove the file by hand. #!/bin/bash # History management hfile="$HOME/.custom_history" HISTCONTROL=...


0

xdotool search --name xyz set_desktop_for_window 4294967295


3

When you call your script like ./myScript.sh myzip test* you give the script the arguments myzip, test1 and test2. The last two arguments are expanded from the test* pattern by the shell before the script is actually invoked. Note that test2 will be available in $3, but your script never uses $3. You can solve this in two ways: Pick off the archive name ...


1

Given the bash-script-template below, you could either add long-options --startand --stop to set variables and use those in an if-statement. you could check if start or stop are in ${remaining_args[@]} and then use that information: if printf '%s\n' ${remaining_args[@]} | grep -Pq '^start$' ; then start your app elif printf '%s\n' ${remaining_args[@]} | ...


0

Probably not the correct way to do it but I ended up doing this: test=0 sudo ./retropie_packages.sh pegasus-fe | while IFS= read i; do ((++stepscomplete)) percentcomplete=$((stepscomplete*3)) if [ $percentcomplete-lt 100 ] then echo $percentcomplete| dialog --hline " TEXT " --backtitle "TESTING..." --gauge "Installing Pegasus" 6 50 else echo 100 | dialog -...


1

check if it contains the sub-string Seems simpler to execute [ -z "${s##*substr*}" ] && echo Substring is present The result of the expansion ${…} is null (zero length) if the substr is present. ... how the condition present in if statement works? The if [[ $STRING1 != "${STRING1/substr/}" ]]; then Is comparing two strings, the original ...


2

You could export your variable and use envsubst: #!/bin/bash export variable=$(df -hP) envsubst '$variable' < my_file.txt If you use the form envsubst < my_file.txt, then all exported variables are substituted. It's safer to explicitly list all your variables you want to substitute, like envsubst '$var1 $var2 $var3' < file Related: Replacing ...


3

That's a convoluted and ksh-specific (though now supported by a few other shells) way to write the standard: case $STRING1 in *substr*) echo Substring is present esac Or ksh-specific: if [[ $STRING1 = *substr* ]]; then echo Substring is present fi ${var/pattern/replacement} is a ksh parameter expansion operator that expands to the value of $var ...


1

With envsubst - replacing your for/cat loop with a while/read loop for the reasons discussed here: Reading lines from a file with bash: for vs. while #!/bin/bash while IFS= read -r i do if [[ -O $i ]] && [[ -d $i ]]; then echo "found it: $i" break else echo "$i is not it." fi done < <(envsubst < mylist.txt) See also ...


0

Keep $HOME out of your file, and use it in your script. For example $ cat mylist.txt most_impt_dir1 most_impt_dir2 most_impt_dir3 misc junk Then: while IFS= read -r i; do dir=$HOME/$i if [[ -O $dir ]] ... ... fi done < mylist.txt Another option, given you're using bash, is to keep the list as a bash array, and source the file to ...


0

You can use xarg ls | xargs -L 1 -d '\n' your-desired-command -L 1 causes pass 1 item at a time -d '\n' make output of ls is split'ed based on new line.


0

There is no way to execute anything on the OS level while a machine is put to sleep, this would kind defeat the purpose of having it asleep in the first place.


-2

Method 1: udiskctl monitor | awk '/IdUUID/{print $NF}' Method 2: udiskctl monitor | sed -n '/IdUUID/p' | sed "s/.*://g" | sed -r "s/^\s+//g"


2

Here's another (run in bash) The python code generates the regular expression and feeds it to grep. grep then works on output from the venerable look utility, which performs a binary search to pull back all the /usr/share/dict/words words that start with q in the example. grep thus has a greatly reduced set of words to search through python3 -c 'import sys ...


5

#!/bin/sh pttrn="^$(printf '%s' "$1" | sed -e 's/\(.\)/\1*/g' -e 's/\*/\\+/' -e 's/\*$/\\+/')"'$' grep "$pttrn" /usr/share/dict/words A pattern is obtained from the first argument by injecting * after each character. Then the first * is changed to \+; so is the last *. Additionally ^ and $ are added. Your example input generates the following pattern: ^q\+...


3

Here's one way to approach it First, filter the wordlist down to just those words that start and end with the same letters as the jumble. For example, if the jumble is passed as positional parameter $1 (and assuming a recent bash shell) grep -x "${1:0:1}.*${1:(-1):1}" /usr/share/dict/words Then take each of these words and blow it apart into a regex - I ...


1

You are probably better off getting a taxonomically correct list of words to start with rather than trying to classify the list you have since people 'in the know' have already done the work. Here's some from Wordnet already classified as verbs, adjectives and nouns etc, though they don't seem to include participles, conjunctions etc etc. The nouns n, ...


2

When you say "I've EOF" I imagine you mean you tried a here document, but have you tried quoting the heredoc word? cat <<'EOF' >FileA for pid in $(ps -lef | grep ssh | grep 2048 | awk '{print $4}'); do kill -9 $pid; done EOF If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and ...


1

You can use a dictionary api, I signed up for a free use token for merriam webster's dictionaryapi.com API. Then you can get random words from the /usr/share/dict/words file (note on macos a lot of the words in this file are not actually real words, or not English words at least.). Since that file doesn't contain any useful information about the word you ...


0

Directly with awk: awk '{ printf "%f\n", $0 }' math.txt The $0 represents the entire line that is read line by line from the file. Additionally, it is not susceptible to nasty injections. It will only evaluate a line as a floating point number.


2

With Perl: perl -ple '$_=eval' ex perl -nE 'say eval' ex with Python: python3 -qi < ex python3 -qic 'import sys; sys.ps1=""' < ex With Haskell: ghci < ex ghci < ex | grep -Po '> \S+$' With calc: calc -f ex # apt install apcalc if necessary


3

If you only need the results, I'd got with the answer provided by @francois-p For fun and games, add paste and sed: $ paste <(sed 's/\($\)/\1\t=/g' somefile) <(bc < somefile) 37 * 60 + 55.52 = 2275.52 34 * 60 + 51.75 = 2091.75 36 * 60 + 2.88 = 2162.88 36 * 60 + 14.94 = 2174.94 36 * 60 + 18.82 = 2178.82 36 * 60 + 8.37 = 2168.37 37 * ...


1

directly in bash/ksh (Edit: As it turns out, bash can't do this, only ksh - thanks for pointing this out): $ while read l > do > echo $(($l)) > done <<! > 37 * 60 + 55.52 > 34 * 60 + 51.75 > 36 * 60 + 2.88 > 36 * 60 + 14.94 > 36 * 60 + 18.82 > 36 * 60 + 8.37 > 37 * 60 + 48.71 > 36 * 60 + 34.17 > 37 * 60 + 42.52 > ...


0

since regex has greedy and non-greedy modes, if you have multiple targets on the same line, it would not extract as you wish. Line: "tom" is a cat, and "jerry" is a mouse. Target: tom jerry Command (greedy mode): grep -oP '".*"' name Command (non-greedy mode): grep -oP '".*?"' name


1

I figured it out. xen-create-image has an option called --role; these roles are defined as sh scripts in /etc/xen-tools/role.d/, and you can create your own, as long as it is executable. I made a really simple role, just for testing: #!/bin/sh prefix=$1 installDebianPackage ${prefix} openssh-server chroot ${prefix} /bin/systemctl enable ssh chroot ${...


4

With old-good Python: $ python -c $'import sys;\nfor line in sys.stdin:print(eval(line))' <math.txt 2275.52 2091.75 2162.88 2174.94 2178.82 2168.37 2268.71 2194.17 2262.52 2271.55 2134.76 2058.9 2029.63 2077.73 2164.49


1

The correct answer is: $ awk '$1=="IdUUID:"{print $2}' file EXTERNALLABEL The currently accepted answer is unnecessarily and incorrectly treating the string you want to search for as if it were a regexp and so that approach would fail if your string contained regexp metacharacters.


12

This awk seems to do the trick: while IFS= read i; do awk "BEGIN { print ($i) }" done < math.txt From here Note that we're using ($i) instead of $i to avoid problems with arithmetic expressions like 1 > 2 (print 1 > 2 would print 1 into a file called 2, while print (1 > 2) prints 0, the result of that arithmetic expression). Note that ...


6

If you have perl: perl -ne 'print eval $_,"\n"' math.txt (I get 50000 lines per second on my laptop using this).


2

Using awk/python: python -c "$(awk '{printf "print %s;", $0}' math.txt)" awk is being used here to format your file into an input that python will accept, then python is doing the work. Alternatively perl can be used in pretty much the same way: perl -le "$(awk '{printf "print %s;", $0}' math.txt)"


8

here is what I whould do not sure it is the best method bc < toto depending what you want to do with datas francois@zaphod:~$ cat > toto 37 * 60 + 55.52 34 * 60 + 51.75 36 * 60 + 2.88 36 * 60 + 14.94 36 * 60 + 18.82 36 * 60 + 8.37 37 * 60 + 48.71 36 * 60 + 34.17 37 * 60 + 42.52 37 * 60 + 51.55 35 * 60 + 34.76 34 * 60 + 18.90 33 * 60 + 49.63 34 * ...


1

This is a frequent question and there are answers on how to do this already, but you can use awk to do this: udiskctl monitor | awk '{if (match($0,"^[[:space:]]*IdUUID:[[:space:]]*([[:print:]]+)",a)) print a[1]}' or, even shorter as suggested by @Archemar udiskctl monitor | awk '/^[[:space:]]*IdUUID:/ {print $2}' or, more robust as suggested by @Ed ...


1

Assuming $arg2 is set to host1 and $arg3 is set to 10.10.10.10 then both your sed commands will remove host1 from the output. The problem is because you are matching 192.168.10.*, the .* at the end will match anything up to the end of the line and replace it with 10.10.10.10. Try: arg2=host1 arg3=10.10.10.10 sed "/${arg2}/ s/192\.168\.10\.[0-9]\+/${arg3}/"...


1

echo "111319 2036"| awk '{print substr($1,1,2)"/"substr($1,3,2)"/"substr($1,5,2) " " substr($NF,1,2)":"substr($NF,3,4)}' output 11/13/19 20:36


1

Independant file descriptor for one or more SSH connections I addition to ControlPath and ControlPersist correct way, proposed by muru's answer, I would like to present an alternative: Method Create a fifo you will use for ssh's outputs You have to create this file in a path you are confident mkfifo $HOME/sshfifo Run long-running command: exec 8> &...


1

I think your interpretation is correct. It was probably an undocumented feature, removed from the undocumented api (see the web archive of the protocol). One workaround is for you to create a symbolic link in the backup directory before the copy. ln -s . folder2


1

You can use cat to concatenate all the files and then pipe them to ssh. cat command1 command2 command3 | ssh root@host 'bash -s'


2

You could try something like this: curl -vX POST --data-urlencode \ "payload={\"text\":\"`git log --pretty=oneline HEAD...$(git tag -l | tail -n2 | head -n1) | tr '\n' ',' | sed 's|,$||'`\"}" \ https://<your-remote>


4

You can use expect script. It can automate ssh connection and run commands on the remote machine. This code should shed some light on automating the ssh connection. you are looking for something like this. store the following code in a file foo.expect #login to the remote machine spawn ssh username@hostname expect "?assword" { send "yourpassword\r"} #...


0

After a lot of search, I have come with an better script, it might be useful for others also. This will display only removable devices like pendrive, etc. This script will work for all users in my centos7 #!/bin/bash for C in `ls /sys/block/`; do RE=`cat /sys/block/$C/removable` if [ $RE == "1" ] ;then #VENDOR=`/bin/udevadm info --...


2

The traditional solution would be to do something with sed sed 's!^\(..\)\(..\)\(..\) \(..\)\(..\)!\1/\2\/\3 \4:\5!' For example: % echo 111319 2036 | sed 's!^\(..\)\(..\)\(..\) \(..\)\(..\)!\1/\2\/\3 \4:\5!' 11/13/19 20:36 There's also the possibility of a pure bash solution. If, for example, you have the value in $a then ${a:0:2}/${a:2:2}/${a:4:2} ${...


3

With GNU awk: awk -v FIELDWIDTHS='2 2 2 1 2 2' '{ print $1 "/" $2 "/" $3 " " $5 ":" $6 }'


7

You could take a hint from a similar question on StackOverflow and use a bash Here document: ssh root@host 'bash -s' << EOF command1 command2 command3 EOF


28

You can use a ControlMaster and ControlPersist to allow a connection to persist after the command has terminated: When used in conjunction with ControlMaster, specifies that the master connection should remain open in the background (waiting for future client connections) after the initial client connection has been closed. If set to no, then the ...


Top 50 recent answers are included