Count occurrences of a char in a string using Bash

you can for example remove all other chars and count the whats remains, like: var=”text,text,text,text” res=”${var//[^,]}” echo “$res” echo “${#res}” will print ,,, 3 or tr -dc ‘,’ <<<“$var” | awk ‘{ print length; }’ or tr -dc ‘,’ <<<“$var” | wc -c #works, but i don’t like wc.. 😉 or awk -F, ‘{print NF-1}’ … Read more

How to cat a file containing code?

You only need a minimal change; single-quote the here-document delimiter after <<. cat <<‘EOF’ >> brightup.sh or equivalently backslash-escape it: cat <<\EOF >>brightup.sh Without quoting, the here document will undergo variable substitution, backticks will be evaluated, etc, like you discovered. If you need to expand some, but not all, values, you need to individually escape … Read more

What does the line “#!/bin/sh” mean in a UNIX shell script?

It’s called a shebang, and tells the parent shell which interpreter should be used to execute the script. #!/bin/sh <——— bourne shell compatible script #!/usr/bin/perl <– perl script #!/usr/bin/php <— php script #!/bin/false <—— do-nothing script, because false returns immediately anyways. Most scripting languages tend to interpret a line starting with # as comment and … Read more

Calling shell functions with xargs

Exporting the function should do it (untested): export -f echo_var seq -f “n%04g” 1 100 | xargs -n 1 -P 10 -I {} bash -c ‘echo_var “$@”‘ _ {} You can use the builtin printf instead of the external seq: printf “n%04g\n” {1..100} | xargs -n 1 -P 10 -I {} bash -c ‘echo_var “$@”‘ … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)