АРХИВ ПО ТЭГУ "BASH"
НОЯБРЬ 14, 2014

Screen Shot 2014 11 13 at 23.00.36 Lottery numbers generator script (bash) %d1%82%d0%b5%d1%85%d0%bd%d0%be%d0%bb%d0%be%d0%b3%d0%b8%d0%b8

Привет,

Решил вот сгенерировать скриптец на баше и поучаствовать в национальной лотерее Великобритании.

#!/bin/bash
  1.  
  2. # Print usage info
  3. function usage() {
  4.   echo "Usage: $0 "
  5.   echo " * lotto   — Lotto"
  6.   echo " * eurom   — Euro Millions"
  7.   echo " * tball   — Thunderball"
  8.   echo " * hotpick — Lotto Hotpicks"
  9.   echo "More info about UK National Lottery visit https://www.national-lottery.co.uk/games/lotto"
  10. }
  11.  
  12. # Check input params
  13. if [ -z "$1" ]; then
  14.   usage
  15.   exit 1
  16. fi
  17.  
  18. type=$1
  19.  
  20. # Generate random number from 1 up to {limit} and print it
  21. function gen_num() {
  22.   limit=$1
  23.   echo -n $[1 + $[ RANDOM % $limit ]]" "
  24. }
  25.  
  26. # Main app
  27. echo -n "Your lucky numbers for $type are: "
  28. case $type in
  29. lotto)
  30.   for i in {1..6}; do gen_num 59; done
  31.   ;;
  32. eurom)
  33.   for i in {1..5}; do gen_num 50; done
  34.   echo -n "+ lucky stars "
  35.   for i in {1..2}; do gen_num 11; done
  36.   ;;
  37. tball)
  38.   for i in {1..5}; do gen_num 39; done
  39.   echo -n "+ thunderball "
  40.   for i in {1..1}; do gen_num 14; done
  41.   ;;
  42. hotpick)
  43.   for i in {1..5}; do gen_num 49; done
  44.   ;;
  45. *)
  46.   usage
  47.   ;;
  48. esac
  49.  
  50. echo ""
  51. echo "Good luck icon smile Lottery numbers generator script (bash) %d1%82%d0%b5%d1%85%d0%bd%d0%be%d0%bb%d0%be%d0%b3%d0%b8%d0%b8 "
ИЮЛЬ 31, 2014

Support multiple (two) directories.

Original file modification date is kept.

Old files are kept as well.

  1.  
  2. #!/bin/bash
  3.  
  4. dir1=a
  5. dir2=bbb
  6. resdir=results
  7.  
  8. # Save file name separator
  9. OLDIFS=$IFS
  10. IFS=$'\n'
  11.  
  12. # File rename by date
  13. echo "Start first processing…"
  14. for fl in $(find "$dir1" "$dir2" -type f); do
  15.     echo " * processing 1: $fl"
  16.     dirpostfix=a
  17.     if [ $(dirname "$fl") == "$dir2" ]; then
  18.         dirpostfix=b
  19.     fi
  20.  
  21.     newfileBase=$(basename "$fl")
  22.     newfile=$(stat -f '%Sm' -t "%Y%m%d%H%M.%S_${dirpostfix}_${newfileBase}" "$fl")
  23.     cp "$fl" "$resdir"/"$newfile"
  24. done
  25. ls -alh "$resdir/"
  26.  
  27. # Process created files
  28. echo ""
  29. echo "Start second processing…"
  30. pushd "$resdir"
  31. i=1
  32. for fl in $(ls); do
  33.     echo " * processing 2: $fl"
  34.  
  35.     fts=$(echo $fl | cut -c3-15)
  36.     newfile=$(seq -f "%03g" $i $i)"_"${fl:16}
  37.    
  38.     mv "$fl" "$newfile"
  39.     touch -t "$fts" "$newfile"
  40.  
  41.     i=$(expr $i + 1)
  42. done
  43. popd
  44.  
  45. # Restore file name separator
  46. IFS=$OLDIFS
  47.  
  48. ls -alh "$resdir"
  49.  
  50. echo ""
  51. echo "Done! Enjoy icon smile File rename with numeric postfix bash script %d1%82%d0%b5%d1%85%d0%bd%d0%be%d0%bb%d0%be%d0%b3%d0%b8%d0%b8 "
  52. echo ""

АПРЕЛЬ 23, 2012

Hi,

Just a note how to get IP address of a host from bash script.

nslookup google.com | grep Add | grep -v ‘#’ | cut -f 2 -d ‘ ‘ | tail -n 1