Tag Info

New answers tagged

2

If you have access to GNU diff you can use its --X-group-format options to get that effect without any additional tools: diff --old-group-format=$'\e[0;31m%<\e[0m' \ --new-group-format=$'\e[0;31m%>\e[0m' \ --unchanged-group-format=$'\e[0;32m%=\e[0m' \ file1 file2 That uses ANSI colour escape codes to get red and green, with ANSI-C ...


0

To test if any of the variables is not 0 use the or operator || (as already suggested): if [[ $result -ne 0 || $resultmax -ne 0 || $resultmin -ne 0 ]] then echo "There is something terribly wrong." fi Though, if you're doing numerical computation and are using ksh (or bash, or zsh) you might prefer to use this syntax for clarity: if (( result != 0 || ...


0

Now you are testing if all variables are not 0 to report error. Try: if [[ $result -ne 0 || $resultmax -ne 0 || $resultmin -ne 0 ]] then echo "There is something terribly wrong." fi


1

If you want to test that one of these variables is not 0, then you need || operator. Not &&. $ if [[ 1 -ne 0 && 0 -ne 0 && 0 -ne 0 ]] ; then echo "There is something terribly wrong."; fi $ if [[ 1 -ne 0 || 0 -ne 0 || 0 -ne 0 ]] ; then echo "There is something terribly wrong."; fi There is something terribly wrong.


2

It depends on the implementation. In ksh there are documented "implementation defined limits" for indexed arrays. For ksh88 there are systems existing with a limit of 1023, for ksh93 the minimum limit required by an implementation is 4095. So you cannot count on having more available than that! (If you are only implementing for a specific system you can test ...


7

i=0 while true; do a[$i]=foo i=$((i+1)) printf "\r%d " $i done This simple script shows on my systems (Gnu/Linux and Solaris): ksh88 limits the size to 2^12-1 (4095). (subscript out of range ). Some older releases like the one on HP-UX limit the size to 1023. ksh93 limits the size of a array to 2^22-1 (4194303), your mileage may vary. bash ...


3

There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Indexed arrays are referenced using integers (including arithmetic expressions (see Shell Arithmetic)) and are zero-based; associative arrays use arbitrary strings. Unless otherwise noted, indexed array indices must be non-negative ...


1

Readability and Style I tend to use the && and || operators quite a lot in my scripting. I even use more than one in a single statement, but only in sections that check whether to continue in the current block. Example1: for word in $list; do condition1 $word || continue condition2 $word || continue : do stuff with $word : do ...


2

In my opinion if-then-else is easier to read for someone who writes in any other language. But my recommendation would be to use the short notation (with && or ||) only for one-liners with only one of the && and ||. Some code like [[ -d mustExist ]] || errorFunction "Dir mustExist is missing" [[ -f toBeSend ]] && sendFile toBeSend ...


7

The problem with that style is that the two forms aren't equivalent. When you use: if command; then foo else bar fi then either foo or bar will be called, never both. When using both && and ||, both paths can be taken: $ [[ -d / ]] && { > echo "Path 1 taken" > false > } || { > echo "Path 2 taken" > } Path 1 taken ...


1

If I understand you correctly you want an indirection in variable's access with, e.g., bash. With this code: content=( a b c d e ) a=10 b=50 c=40 d=90 e=100 echo "${!content[0]}" echo "${!content[1]}" echo "${!content[2]}" echo "${!content[3]}" echo "${!content[4]}" You will get this result: 10 50 40 90 100 The key here is bash's specific variable ...


1

So I'll define an indirect printing function first... _print() while [ "$#" -ne 0 ] do printf '$%s = %d\n' \ "$1" "$(($1))" shift done Next I'll setup the array and increment... arr=( a b c d e ); i=0 for var in "${arr[@]}" do : "$(($var=(i+=10)))" done So now the value of $a is 10 and $b 20 ...


1

Try #i=0 for var in 'a' 'b' 'c' 'd' 'e' do content[${#content[*]}]=$var # or content[i++]=$var done # or just content=( 'a' 'b' 'c' 'd' 'e' ) a=10 b=50 c=40 d=90 e=100 for i in "${#content[@]}" do echo ${!i} done


2

Because arrays management in korn shell is far away from other programming languages you can try something like: Instead of this: while [ $a -le 9 ] do ${array_FileName_category[$a]}_file=$TEMPDIR/${array_FileName_category[$a]}_file_$$ a=`expr $a + 1` done try this: while [ $a -le 9 ] do b=$array_FileName_category[$a] touch $TEMPDIR/${b}_file_$$ a=`expr ...


1

Welcome to AIX. On of the things that is ubiquitous in IBM operating systems is the idea that system messages all have identifying alphanumeric codes. The messages may be translated into different languages, but the codes stay the same. And there is usually, as part of IBM's documentation, a reference manual where one can go and look up these messages by ...


3

you need a space after '[' because '[' is a command see here http://stackoverflow.com/questions/9581064/why-should-be-there-a-space-after-and-before-in-the-bash-script You also need ${} around the array variable reference, so you should have: source_array_list[0]="a" source_array_list[1]="b" source_array_list[2]="c" source_array_list[3]="d" ...


1

If there is no chance that any of the characters <> | might occur within the data you wish to retain in your file, then almost definitely the most efficient solution is simply to transliterate them away entirely: tr '<>|' ' ' <infile | ###translate all delimiter chars to spaces tr -s ' ' >outfile ###pipe results to second tr ...


2

Try this way: sed -e 's:<>\s\|.*:Replaced:g' filename That's how the result looks like: $ echo "<> |" | sed -e 's:<>\s\|.*:Replaced:g' Replaced If you just want to remove it, delete word Replaced from the command. If there is an space between symbols < and >, you should replace <> with <\s>. After the question ...


1

grep "1234-5678" * -r or grep "1234-5678" * -R if you want to follow the symbolic links.


2

Portably/standardly: find . -type f -exec grep 1234-5678 /dev/null {} + Some grep implementations have -r or -R options to search in files recursively. The behaviour varies from implementation to implementation though. With the grep found in AIX 6.1 for instance, you'll probably want to use the -R option1. Beware though that contrary to the find ...


3

Bash has the -s option to test for existence and size greater than zero: -s file True if file exists and has a size greater than zero. so you can do if [ -s "${array_export_files[$loopcount]}" ]; then sed ....... fi within the loop. Since the if [ "$loopcount" -eq "$loopcount" ] is always true you can just replace that: while [ ...


3

The problem is that the typeset creates the return status. This code (i.e. both assignments in the same declaration) works for me: typeset -r command_output=$(command) return_status=$? To keep the return_status writable (not read-only) you can do: typeset command_output=$(command) return_status=$? typeset -r command_output (i.e. declare the ...



Top 50 recent answers are included