Tag Info

Hot answers tagged

6

You can't print text with instructions to change its colour repeatedly. You can write different-coloured text over the top of your original text, with something like this: i=0 while true do echo -en "\r\e[1;3${i}mWelcome to the Matrix\e[0m" i=$(((i+1)%8)) sleep 0.25 done That will cycle through all the bright colours, changing four times a ...


5

Your function has an exit status but no output. Your variable $a will always be empty, so the [[ $a ]] test will always be "false" You truly want this: if is_equal 42; then ... But what you think you want is this is_equal 42 # don't capture the output a=$? # but do grab the exit status if [[ $a -eq 0 ]]; then ...


3

I see what is happening with your case statements. From the Pattern Matching section of the bash man page: [...] Matches any one of the enclosed characters. For all of the hours from 10-23, the pattern matching is looking for any one of the enclosed characters. Option 1: 1[0-2]) apexctl ... ;; 1[3]) apexctl ... ;; Option 2: 10|11|12) apexctl ... ...


3

You completely misunderstand the way case and [ ] work. What you need is if ... elif ...: if [ "$zed" -eq 0 ] && [ "$zed" -le 3 ]; then : ... elif [ "$zed" -gt 3 ] && [ "$zed" -lt 12 ]; then : ... elif [ "$zed" -eq 27 ]; then : ... else : ... fi [ "$zed" -eq 0] && [ "$zed" -le 3 ] doesn't make sense anyway because 0 ...


3

The simple solution here to use some kind of interprocess communication to tell the target process to make the change. For example, you could use signals: #!/bin/sh export x=true start() { trap x=false USR1 while [ $x = "true" ]; do echo running... sleep 1 done } start & sleep 5 kill -USR1 $! wait produces this output: $ ./testit.sh ...


3

You get an error because you're attempting to do arithmetic equality with string values. Here are 2 ways to check whether the elements of dfArray are in dsmArray set -A dfArray / /usr /var /tmp ... set -A dsmArray /home /opt /usr ... for a in "${dfArray[@]}"; do in=false for b in "${dsmArray[@]}"; do if [[ $a == $b ]]; then echo "$a is in ...


3

for((i=2;i<=$#;i++)); do wc "${!i}" done


2

If you put set -xv in your script, you might be able to detect why there is an error. In your script, it will output this line: +for divider in '{2..$a}' Notice the expansion did not occur. Read through the linked duplicates to identify how to fix it.


2

So, when is it important to write portable scripts? what types of scripts should be as portable as possible? When you're using them for your working environment, and you work (or could in the future) on different machines - AND, you don't want to have to rewrite your tools prior to getting down to work. eg: a trash script / rm replacement Mark ...


2

UPDATED: simpified and tested in gawk (v.3.1.8) and mawk (v. 1.3.3) This awk script should do what you seem to be asking for; it may work on other awk implementations that support regular expression field- and record-separators: #!/usr/bin/awk -f BEGIN { RS="}\n\n?" ORS="}\n" FS="\n" OFS="\n" } { # compress whitespace in first field gsub(/[ ...


2

I would iterate in decreasing order. The loop can terminate sooner. for ((check=12; check>1; --check)) { instead of for ((check=2; check<13; check++)) { Just think about it. If the number is not divisible by 12, it's not divisible by 4, 3 and 2 either. Not vice versa.


2

You can leverage the fact that (( returns zero when the result is non-zero: if (( $1 % check )); then ... fi


2

You can do it with awk awk -F= '/^frame/{line=FILENAME "=" $2}NR!=1 && FNR==1{print line}END{print line}' *.txt Or with sed (GNU version more than 4.2.2) sed -sn '/^frame/{s///;h};${F;x;p}' *.txt | sed 'N;s/\n//'


2

This is very simple. You don't need delimiters as such, a simple regular expression will do. Just look for an opening [, followed by as many non-] or [ characters as possible until the end of the line. For example: Perl If you know there are no [[ or other strange things: perl -pe 's/\[.+?\]//g' file If you can have strange things: perl -pe ...


2

may try: gnome-terminal -e "bash -c 'watch sensors'" gnome-terminal -e "bash -c 'gedit /etc/hostname'" gnome-terminal -e "bash -c 'processing /home/Desktop/samudra_gui/samudra_gui.pde'" and start this script from .bashrc or write this in .bashrc


2

for f do exec <"$f" : handle stdin done A non-interactive shell will treat any redirection from a file that cannot be read or that does not exist when associated w/ a special builtin as a fatal error and exit immediately with a meaningful diagnostic message written to stderr. So either your parameters are valid, readable files and the above ...


2

You can "subscript" that pseudo-array. It starts with $1, so to iterate over $2, $3, ... for arg in "${@:2}"; do echo "$arg" done


2

Try: $ awk 'NF{$NF = sprintf("%.6f", $NF*3)}1' file Data 9390.900391 10573.089844 80.000000 200.000000 8.100000 Data 17762.810547 18536.189453 85.000000 200.000000 8.100000 Change $NF to the $n where nis the field you want to change.


2

{1..$num_in} is a kshism/zshism. You should write: `seq $num_in` Note: Though bash supports code like {1..3}, as said by 1_CR in comment, {1..$num_in} doesn't work in bash, due to the fact that brace expansion precedes parameter substitution. So, it probably comes from ksh93 or zsh, where it works because parameter expansion is done first.


2

When you type function_under_test, the shell think it's a command, not a variable. You need to expand it, so function_uneder_test will be expanded to sum_squares. Change your line 32 to: "$function_under_test" "$3"


1

awk -F'[[:blank:]|]+' ' /^Mikrotik01/ {if (NR>1) print ""; printf "%s", $(NF-2)} /Unique Entry/ {printf ",%s", $NF} END {print ""} ' file.txt > File.csv


1

my understanding is that the second part of your code give a PID, and you want to grep it. and also the double ps is a typo. something like while read -r -u10 server port line do echo ========== server: "$server" port: "$port" ========== pid=$(ssh -qn "$server" "netstat -tulpunt | grep -E ":$port "" | \ awk '{print $7}' | grep '/' | awk -F ...


1

With grep: diff -c file1 file2 | grep -v '^ ' none of the other lines start with two spaces: not the ones starting with !, and not the line indications.


1

with grep: diff -c file1 file2 | grep '^[-!*]'` with sed: diff -c file1 file2 | sed '/^[-!*]/!d'


1

With find + grep: find . -name '*.txt' -exec sh -c 'grep -HPo "frames=\K.*" "$0" | tail -n1' {} \; And with shell for loop + grep in similar fashion: for file in *.txt; do grep -HPo 'frames=\K.*' "$file" | tail -n1; done


1

Using gawk and provided that there will only be one = on the line, you can do: gawk -F= 'ENDFILE { print FILENAME"="$2 }' *.txt Update Just realised I misread this question, I thought the line you were looking for was always be the last line. To use the last line that starts with frames=, for gawk do: gawk -F= '/^frames=/ { frames=$2 } ENDFILE { print ...


1

I downloaded this installer and took a peek. Here are the relevant code snippets: #!/bin/bash MD5=1fe8463da9322ad9c966a57b92569a52 # The required tools: SED=sed MD5SUM=md5sum CUT=cut DATA="$SED -e '1,/^__DATA__$/d' '$0'" # Check archive integrity: test `eval $DATA | $MD5SUM | $CUT -d " " -f 1` = $MD5 || { echo "Checksum error in '$0'" >&2 ...


1

Assuming the goal here is to find the lowest number divisible by integers 2 through n, trying to check every integer is massively inefficient. The easiest algorithm I can think of is to use an array of factors that will eventually be multiplied together to get the answer. It starts empty. Then, take each number in sequence, and divide out the factors that ...


1

You are right that there are no goto commands but there are control loops with all the standard features. In your case, you just want to loop indefinitely: while sleep 1m do your commands here .... done


1

Ok the easieet way to blobk that log line is to not use cron to launch your script. Have your script re-run itself every 60 seconds instead. then it need only be launched once, eg by /etc/inittab.



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