Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of github.com:liweinan/recipies

  • Loading branch information...
commit b591565dc5cdc516c853a3f5820b3c3aae196ee6 2 parents 8410628 + 6eda8fd
Weinan Li authored
16 bash/here_str/1.sh
... ... @@ -0,0 +1,16 @@
  1 +String="This is a string of words."
  2 +
  3 +read -r -a Words <<< "$String"
  4 +# The -a option to "read"
  5 +#+ assigns the resulting values to successive members of an array.
  6 +
  7 +echo "First word in String is: ${Words[0]}" # This
  8 +echo "Second word in String is: ${Words[1]}" # is
  9 +echo "Third word in String is: ${Words[2]}" # a
  10 +echo "Fourth word in String is: ${Words[3]}" # string
  11 +echo "Fifth word in String is: ${Words[4]}" # of
  12 +echo "Sixth word in String is: ${Words[5]}" # words.
  13 +echo "Seventh word in String is: ${Words[6]}" # (null)
  14 + # Past end of $String.
  15 +
  16 +# Thank you, Francisco Lobo, for the suggestion.
13 bash/opts2.sh
... ... @@ -0,0 +1,13 @@
  1 +#!/bin/bash
  2 +vars=(a b c)
  3 +
  4 +while getopts ":a" opt "${vars[@]}"; do
  5 + case $opt in
  6 + a)
  7 + echo "-a was triggered!" >&2
  8 + ;;
  9 + \?)
  10 + echo "Invalid option: -$OPTARG $opt" >&2
  11 + ;;
  12 + esac
  13 +done
28 lpi/5-1.c
... ... @@ -0,0 +1,28 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <ioctl.h>
  4 +
  5 +void errExit(char* reason) {
  6 + exit(-1);
  7 +}
  8 +
  9 +int main(int argc, char* argv) {
  10 + fd = open(argv[1], O_WRONLY); /* Open 1: check if file exists */
  11 + if (fd != -1) { /* Open succeeded */
  12 + printf("[PID %ld] File \"%s\" already exists\n", (long) getpid(), argv[1]);
  13 + return close(fd);
  14 + } else {
  15 + if (errno != ENOENT) {
  16 + errExit("open");
  17 + /* Failed for unexpected reason */
  18 + } else {
  19 + /* WINDOW FOR FAILURE */
  20 + fd = open(argv[1], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  21 + if (fd == -1)
  22 + errExit("open");
  23 + printf("[PID %ld] Created file \"%s\" exclusively\n",
  24 + (long) getpid(), argv[1]);
  25 + /* MAY NOT BE TRUE! */
  26 + }
  27 + }
  28 +}
2  make/Makefile
... ... @@ -0,0 +1,2 @@
  1 +c-files:
  2 + @echo $(sort $(dir $(shell find . -name '*.c')))
18 make/assert/Makefile
... ... @@ -0,0 +1,18 @@
  1 +# $(call assert,condition,message)
  2 +define assert
  3 + $(if $1,$(error Assertion True),$(error Assertion failed: $2))
  4 +endef
  5 +
  6 +# $(call assert-file-exists,wildcard-pattern)
  7 +define assert-file-exists
  8 + $(call assert,$(wildcard $1),$1 does not exist)
  9 +endef
  10 +
  11 +# $(call assert-not-null,make-variable)
  12 +define assert-not-null
  13 + $(call assert,$($1),The variable "$1" is null)
  14 +endef
  15 +
  16 +AAA='aaa'
  17 +error-exit:
  18 + $(call assert-not-null,AAA)
25 make/debug/Makefile
... ... @@ -0,0 +1,25 @@
  1 +# $(debug-enter)
  2 +debug-enter = $(if $(debug_trace), $(warning "Entering $0($(echo-args))"))
  3 +
  4 +# $(debug-leave)
  5 +debug-leave = $(if $(debug_trace), $(warning Leaving $0))
  6 +
  7 +comma := ,
  8 +echo-args = $(subst ' ','$(comma) ', $(foreach a,1 2 3 4 5 6 7 8 9,'$($a)'))
  9 +
  10 +debug_trace = 1
  11 +
  12 +define a
  13 + $(debug-enter)
  14 + @echo $1 $2 $3
  15 + $(debug-leave)
  16 +endef
  17 +
  18 +define b
  19 + $(debug-enter)
  20 + $(call a,$1,$2,hi)
  21 + $(debug-leave)
  22 +endef
  23 +
  24 +trace-macro:
  25 + $(call b,5,$(MAKE),c,d,e,f,g,h,i,j,k,l)
22 make/filter/Makefile
... ... @@ -0,0 +1,22 @@
  1 +words := he the hen other the%
  2 +
  3 +.PHONY: get-the
  4 +
  5 +get-the:
  6 + @echo $(filter "he", $(words))
  7 + @echo $(filter %he, $(words))
  8 + @echo $(filter he%, $(words))
  9 + @echo $(filter %he%, $(words))
  10 +
  11 +sources := count_words.c counter.c lexer.c
  12 +objects := $(subst .c,.o,$(sources))
  13 +
  14 +.PHONY: replace
  15 +
  16 +replace:
  17 + @echo $(objects)
  18 +
  19 +.PHONY: dollar
  20 +
  21 +dollar:
  22 + @echo $$
24 make/func/Makefile
... ... @@ -0,0 +1,24 @@
  1 +define free-space
  2 + df
  3 +endef
  4 +
  5 +define parent
  6 + echo "parent has two params: $1, $2"
  7 + $(call child,$1)
  8 +endef
  9 +
  10 +define child
  11 + echo "child has one param: $1"
  12 + echo "but child can also see parent's second param: $2!"
  13 +endef
  14 +
  15 +.PHONY: scoping_issue default
  16 +
  17 +scoping_issue:
  18 + @$(call parent,one,two)
  19 +
  20 +default:
  21 + $(free-space)
  22 +
  23 +wildcard:
  24 + @echo $(wildcard M*)
15 make/global_var/Makefile
... ... @@ -0,0 +1,15 @@
  1 +DATE = date
  2 +.PHONY: a
  3 +a:
  4 + @$(var1)
  5 + @sleep 1
  6 + @$(var1)
  7 +
  8 +var1 = $(DATE)
  9 +
  10 +b:
  11 + @$(var2)
  12 + @sleep 1
  13 + @$(var2)
  14 +
  15 +var2 := $(DATE)
11 make/inc/Makefile
... ... @@ -0,0 +1,11 @@
  1 +include my.inc
  2 +.PHONY: clean gen
  3 +my.inc:
  4 + @echo "var = abc" > $@
  5 +
  6 +out: my.inc
  7 + @echo $(var)
  8 +
  9 +clean:
  10 + @rm -rf my.inc
  11 +
1  make/inc/my.inc
... ... @@ -0,0 +1 @@
  1 +var = abc
14 make/scope/Makefile
... ... @@ -0,0 +1,14 @@
  1 +define f1
  2 + f1
  3 +endef
  4 +
  5 +scope:
  6 + @echo $(origin f1)
  7 + @echo $(origin origin)
  8 + @echo $(call f1)
  9 +
  10 +letters := $(foreach letter,a b c d,$(addprefix 'P',$(letter)))
  11 +
  12 +.PHONY: pl
  13 +pl:
  14 + @echo $(letters)
10 make/target_var/Makefile
... ... @@ -0,0 +1,10 @@
  1 +var = 'XY'
  2 +
  3 +.PHONY: a b
  4 +
  5 +a: var += 'Z'
  6 +a:
  7 + @echo $(var)
  8 +
  9 +b:
  10 + @echo $(var)
11 make/vars/Makefile
... ... @@ -1,3 +1,14 @@
  1 +.PHONY: dummy
  2 +
  3 +dummy:
  4 + @echo $(MAKE_VERSION)
  5 + @echo $(CURDIR)
  6 + @echo "$(.VARIABLES)"
  7 + @echo $(COMPILE.C)
  8 + @echo $(CXX)
  9 + @echo $(CXXFLAGS)
  10 + @echo $(TARGET_ARCH)
  11 +
1 12 default: pre pre2 pre2
2 13 @echo $+
3 14 @echo $<

0 comments on commit b591565

Please sign in to comment.
Something went wrong with that request. Please try again.