« first day (577 days earlier)      last day (2707 days later) » 

06:20
@RompePC ... not sure what you want
@I0_ol you can get the linux terminal to colorize text but colorizing the output od querys is something that the sql client should ask the terminal to do
@louigi600 I've a own bash function called mkenv, which is a shortcut for doing a virtualenv ... and a mkdir ..., and I wanted to add a --help command. But I'm pretty sure I can't do it without checking the $1 value inside the function, and then do an echo printing info about.
but .bashrc is automatically loaded bye a login shell ... you want to add a help menu bfor running it manually to do other stuff .... I suppose
I don't do long option parsing but here's a code segment that deals with option parsing in shell script:
Just add an echo like you do with mkdir --help.
06:35
getopt $GETOPT_STRING $* >/dev/null 2>/dev/null || \
  usage unknown option or insufficient parameters: $*
GPO=$(getopt $GETOPT_STRING $* 2>/dev/null |tr -d "'")
set -- $GPO
case $1 in
    -a) set_myoptions_value a 1; shift ;;
    -r) set_myoptions_value r 1; shift ;;
    -h) usage ;;
    -k) set_myoptions_value k 1; shift  ;;
    -g) set_myoptions_value g $2; shift 2 ;;
    -G) set_myoptions_value G $2; shift 2 ;;
    -M) set_myoptions_value M $2; RU=$2 ; shift 2 ;;
    --) shift ; break ;;
    *) usage $1 unknown option ;;
the GETOPT_STRING for that wold look like "arhkg:G:M:"
but I generate it automatically fom other stuff
MYOPTIONS=(
"a,,\t\t:Create local personal users for sysdbsm,0"
"r,,\t\t:Reset user passwords instead of creating accounts,0"
"h,,\t\t:Show this help message,0"
"k,,\t\t:create ssh rsa keys,0"
"g,:,\t:Creat accounts with specified primary group,"
"G,:,\t:Creat accounts with specified additional groups (colon separated list),"
"M,:,\t:Use this as the mail relay instead of default (ip:port),"
)
usage ()
{ cat <<EOF

usage: $NAME [OPTIONS]

OPTIONS
EOF
  I=0
  while [ $I -lt ${#MYOPTIONS[*]} ]
  do
    OPTION=$(echo "${MYOPTIONS[$I]}" |cut -d "," -f 1)
    PARM=$(echo "${MYOPTIONS[$I]}" |cut -d "," -f 2)
    HTXT=$(echo "${MYOPTIONS[$I]}" |cut -d "," -f 3)
    [ "$PARM" = ":" ] && echo -e "-$OPTION <value> $HTXT" || echo -e "-$OPTION $HTXT"
    I=$(expr $I + 1)
  done

  echo
  if [ "$*" != "" ]
  then
    echo "Error: $*"
    exit 1
  else
    exit 0
  fi
}
GETOPT_STRING=$(I=0
  while [ $I -lt ${#MYOPTIONS[*]} ]
  do
    OPTION=$(echo "${MYOPTIONS[$I]}" |cut -d "," -f 1)
    PARM=$(echo "${MYOPTIONS[$I]}" |cut -d "," -f 2)
    echo -en "${OPTION}$PARM"
    I=$(expr $I + 1)
  done
)
I'm sure tripleee will be horrified by some of this code ... but it's rather ols and maybe one day I'll revisit it to make it less horrific
Uf, I'll try to understand it when I get back home and have some time to spare. Thanks!
I'll also check if it can be refactorized as you say.
getopt is an option parser .... it's handy to check if the options you gave are valid
the crunch of it is in the case which handles the options:
case $1 in
  -a) set_myoptions_value a 1; shift ;;
  -r) set_myoptions_value r 1; shift ;;
  -h) usage ;;
  -k) set_myoptions_value k 1; shift  ;;
  -g) set_myoptions_value g $2; shift 2 ;;
  -G) set_myoptions_value G $2; shift 2 ;;
  -M) set_myoptions_value M $2; RU=$2 ; shift 2 ;;
  --) shift ; break ;;
  *) usage $1 unknown option ;;
esac
no instead of manipulating other stiff you could have the case actually do something
-h) echo "print this help message" ;;
I showed you the MYOPTIONS thing because I find that handy to minimize code as with that approach the usage function output is self generated from the MYOPTIONS array and so is the GETOPT_STRING
I actually have that case in a while ... that's why I shift the parameters:
while [ $# -ge 1 ]
do
  case $1 in
    -a) set_myoptions_value a 1; shift ;;
    -r) set_myoptions_value r 1; shift ;;
    -h) usage ;;
    -k) set_myoptions_value k 1; shift  ;;
    -g) set_myoptions_value g $2; shift 2 ;;
    -G) set_myoptions_value G $2; shift 2 ;;
    -M) set_myoptions_value M $2; RU=$2 ; shift 2 ;;
    --) shift ; break ;;
    *) usage $1 unknown option ;;
  esac
done
07:43
@louigi600 I don't understand what sql has to do with getting color from a select loop.
ooops ... I interpreted select as in "select * from somefable;"
well then you ask the terminal to render some text in color
yeah but it's not easy in a select loop
it gets all wonky
and really what i'm trying to do is color only the numbers but not the items
782
Q: How to change the output color of echo in Linux

satheesh.droidI am trying to print a text in the terminal using echo command. I want to print the text in a red color. How can I do it?

you willm need to print text change color , print number, and reset color to normal
I know how to print color to the terminal
but with a select loop it's not so simple
echo -e 'hello \033[0;31m 1234567 \033[0;37 mack to normal'
have something like that inside your loop
where the number part is what whatever you need .... and might eben be in a variavle
for A in 1 2 3 4 5 6 7 8 9
do
  echo -e 'hello \033[0;31m $A \033[0;37 mack to normal'
done
07:53
ok try this.. select f in *; do echo "\033[0;31m $f \033[0m"; done
in a for loop it works fine.. in a SELECT loop it's not so easy.
I'm not sure what you're expanding to generate a list ... so I thaught the for loop woyld ve the easiest thing fro an example
in any case if you want to change the colors of what select is outputting I think you'll haveto do it by hand without using the shell builtin
I've actually been able to get color in the output but it messes up the column spacing and everything get out of whack.
yes but I dont think you can get it to to colorize gust the number ....
I'd just rather use select than emulate it since it does such a nice job
yeah that's what I think too but I thought I'd ask around you know
in any case you need to have echo -e to enable the interpretation od the special characters
08:06
It seems like you could colorize the numbers though by using a case statement like.. select opt in ${options[@]}; do case "\033[0;31m $REPLY \033[0m" in [0-9]) echo $opt;; esac; done
but that doesn't work
    I don't use much select but I do much the same thing here:
 AVALIBLE_KERNELS=( $(file $(ls -rt) |grep -v "boot\..*" |awk -F: '/Linux kernel/ || /x86 boot sector/ {printf("%s ",$1)}') )
 echo ${#AVALIBLE_KERNELS[*]}

 for ((i=0;i<${#AVALIBLE_KERNELS[*]};i++))
 do
   echo "[${i}] ${AVALIBLE_KERNELS[$i]}"
 done
  echo -n "Please choose for which kernel image should be used for the new initrd  <0-$((${#AVALIBLE_KERNELS[*]} -1))> ? : [$((${#AVALIBLE_KERNELS[*]} -1))]"
with that approach it should be relatively easy to colorize the number part
for ((i=0;i<${#AVALIBLE_KERNELS[*]};i++)); do echo -e "\033[0;31m[${i}]\033[0;37m ${AVALIBLE_KERNELS[$i]}"; done
08:28
actually I've since learned that there's an unnecessary grep
AVALIBLE_KERNELS=( $(file $(ls -rt) |awk -F: '/Linux kernel/ || /x86 boot sector/ && !/boot\..*/ {printf("%s ",$1)}') )
Try this and you'll see what I mean. Even doing this in a for loop makes the output crazy.
bold(){
printf '%b' "\033[38;1m$@\033[0m"
}
mapfile -t options < <(gshuf -n50 /usr/share/dict/words)

n=${#options[@]}
i=0

until [[ $i -eq $n ]]; do
for opt in "${options[@]}"; do
i=$((i+1))
bold "$i) "
printf '%s\n' "$opt"
done
done | column
notice it's gshuf by the way
you might need to change that on yours
And sorry about the formatting.. I don't know how to format it here.
But yeah I should probably just ask a question on the main site. It's frustrating enough to be a real question. :)
08:44
The way I showed you does this (with the [number] in red:
[0] vmlinuz-huge-smp-4.4.13-smp
[1] vmlinuz-huge-smp-4.4.14-smp
[2] 4.4.13-smp
[3] 4.4.14-smp
[4] vmlinuz-huge-smp-4.4.29-smp
[5] 4.4.29-smp
no crazuness in that
the craziness comes from having multiple columns. when it's all in a single column it's fine. but i'm not working with a single column.
09:03
gshuf as in : shuf - generate random permutations
09:34
@I0_ol You probabbly meant notid mapfile/readarray it might do part of what you were doing
I could further optimize as thus:
declare -a AVALIBLE_KERNELS
readarray -t AVALIBLE_KERNELS<<< $(file $(ls -rt) |grep -v "boot\..*" |awk -F: '/Linux kernel/ || /x86 boot sector/ {printf("%s ",$1)}')
but that stuffs eberything in the first element
not exacty what I wanted
even with the \n in the awk printf
aha I get it ....
readarray -t AVALIBLE_KERNELS< <(file $(ls -rt) |grep -v "boot\..*" |awk -F: '/Linux kernel/ || /x86 boot sector/ {printf("%s \n",$1)}')
I figured it out. Part of the problem was the Coderunner terminal making a mess of things that my normal terminal handles just fine. As in the case of the for loop I posted. It works fine when output to a normal terminal.. I'm dumb. :)
The select loop still doesn't output color but that's ok.
I don't think it ever will output color no matter how much I want it to :)
@louigi600 I have no idea what you're doing but it looks terrifying ;)
09:51
recreating initrd ...
not terrifying .... just have a non standard system with encrypder PV and use a custom initrd that I need to recreate every time I recompile the kernel (or upgrade from distro)
this is the whole thing:
#!/bin/bash
MODULES="-m usb-storage:ehci-hcd:usbhid:hid_generic:hid:uhci-hcd:jbd2:mbcache:ext4:i2c_hid:hid_multitouch:ehci-pci"
get_kernel_version ()
{ kver $1 | awk '{print $1}'
}

declare -a AVALIBLE_KERNELS
readarray -t AVALIBLE_KERNELS<  <(file $(ls -rt) |grep -v "boot\..*" |awk -F: '/Linux kernel/ || /x86 boot sector/ {printf("%s\n",$1)}')

echo ${#AVALIBLE_KERNELS[*]}

for ((i=0;i<${#AVALIBLE_KERNELS[*]};i++))
do
  echo "[${i}] ${AVALIBLE_KERNELS[$i]}"
done
echo -n "Please choose for which kernel image should be used for the new initrd <0-$((${#AVALIBLE_KERNEL
@I0_ol if you want you can have the entire output of select in a certain colour ....
I think you can .... but it may not even be what you want
@louigi600 I'm just gonna use a for loop to mimic select.
@tripleee can you explai a little better why
ls | readarray -t
does not work and you need to do it this way
readarray -t < <(ls)
?
the man says that readarray reads from stdin so why is
ls | readarray -t
not working right ?
@I0_ol like I did
hi, i have a simple question:
do you know why does Vim mark $(variablename) syntax as error?
10:08
@louigi600 sure
10:47
you should not be using ls in scripts anyway
3
Q: Bash: pipe 'find' output into 'readarray'

villapxI'm trying to search for files using find, and put those files into a Bash array so that I can do other operations on them (e.g. ls or grep them). But I can't figure out why readarray isn't reading the find output as it's piped into it. Say I have two files in the current directory, file1.txt an...

TL;DR the readarray runs in a subshell so you lose it
there is a broader FAQ about read etc in a pipeline
@Grugnas I don't use vim so not sure exactly how to repro ... but why would you want to use a variable name as a command?
actually I'm guessing you are confused about the difference between ${variable} and $(command) .... hang on
22
A: Difference between ${} and $() in bash scripting

tripleeeThe syntax is token-level, so the meaning of the dollar sign depends on the token it's in. The expression $(command) is a modern synonym for `command` which stands for process substitution; it means, run command and put its output here. So echo "Today is $(date). A fine day." will run the date...

(not a brilliant question, unsure why I keep getting upvotes on that answer)
@RompePC the short answer is no, you just have to add code to do that
the getopt answer is severe overkill if that's all you need; I find myself doing stuff like case $1 in --help | -[?h]) echo "Syntax: $0 <blarg>" >&2; exit 1;; esac and not attempting to support multiple options or mixing --help with other arguments
(not really here -- need to finish a drop before going on vacation)
 
1 hour later…
12:00
@tripleee
in this way
file $(ls -rt) |grep -v "boot\..*" |awk -F: '/Linux kernel/ || /x86 boot sector/ {printf("%s\n",$1)}'
or in this other way
readarray -t < <(ls)

the forme I will mend ... the latter was just an example
12:18
the former I also got further reduced to
file $(ls -rt) |awk -F: '/Linux kernel/ || /x86 boot sector/ && !/boot\..*/ {printf("%s\n",$1)}'
I can't recall why I preferred using ls -rt over find
12:37
maybe because find was much slower ? or because i did not want to look at the man page to omit printinf the leading ./ ?
@louigi600 printing the leading ./is a feature because otherwise you get trouble with files whose name starts with a dash (file: invalid option -- u for a file named ./-uck for example)
if you only want to examine the current directory, find -maxdepth 1 should actually be faster than ls because it doesn't sort the files, though then you are left to reimplement -rt separately
you can have find print the modification date and sort on that, then discard that field, of course, but I agree that there is no really elegant solution
mywiki.wooledge.org/ParsingLs is the canonical for why not use ls; there's a number of contentious issues there
anyway, have fun -- I probably won't be around much for the next couple of weeks
... though I'll be sure to pop in for mod elections on Monday-ish
12:53
I'll be on vacation too
I know it's a feature but in this subcase it was getting in the way
@tripleee would you approve this:
find . -type f -exec file {} \; |awk -F: '/Linux kernel/ || /x86 boot sector/ && !/boot\..*/ {printf("%s\n",$1)}
or can I make that even better ?
oh and BTW I try not to put silly stuff in the names of the kernels I put in boot ... to make it obvious what it is I generally cal them with the kernel full release numbers
13:53
@trip
@tripleee : Hello
I am trying to copy Java installtion directory using shell script like below
sudo cp -rf "$DIR/content/jdk1.8.0_131.jdk" /Library/Java/JavaVirtualMachines
this works fine on my mac.... whereas on another mac the directory cannot be opened...it says user do not have permission to view the contents
do we need to specifiy something in the shell script for this?
while copying files
 
3 hours later…
17:23
@icodes sounds like an ACL problem; see vaguely this discussions.apple.com/thread/7350269?start=0&tstart=0
@louigi600 if you want to trim the ./ you can do { print substr($1, 3) }
and maybe add -maxdepth 1 to prevent it from traversing subdirectories

« first day (577 days earlier)      last day (2707 days later) »