LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need some shell scripting help (https://www.linuxquestions.org/questions/linux-newbie-8/need-some-shell-scripting-help-4175561367/)

like100ninjas 12-13-2015 01:44 PM

need some shell scripting help
 
edit: once again, trying to make this lemonbar script work. i fixed all the error messages, and the script runs. i think my problem might be that i'm testing `read -r line' in a subshell, so the script never really parses the lines i'm trying to read. just a hunch. anyone know how to fix this? or maybe something else is wrong.

edit again: i keep trying different things but no text shows up on the bar still. i've tried adding "print $line" underneath the while line and nothing shows up.

$HOME/etc/lemonbar/panel_bar
Code:

#!/usr/bin/zsh

source $HOME/etc/lemonbar/config

while [[ -n $( read -r line ) ]] {
  case $line {
    S*)
      panel_status="{F$panel_c7}${line#?}{F-}"
      ;;
    T*)
      panel_title="{F$panel_c7}${line#?}{F-}"
      ;;
    W*)
      panel_info=''
      panel_data=${=line}
      for i in ${panel_data}; {
        case ${panel_data[$i]} {
          OFU*)
            panel_info="$panel_info{F$panel_c2} {F-}"
            ;;
          o*)
            panel_info="$panel_info{F$panel_c7} {F-}"
            ;;
          f*)
            panel_info="$panel_info{F$panel_c8} %{F-}"
            ;;
          u*)
            panel_info="$panel_info%{F$panel_c3} %{F-}"
            ;;
        }
      }
      ;;
  } 
  print "%{l}$panel_title%{c}$panel_info%{r}$panel_status"
}

$HOME/etc/lemonbar/panel
Code:

#!/usr/bin/zsh

source $HOME/etc/lemonbar/config

if [[ $(pgrep -cx panel) -gt 1 ]] {
    print -P "%F{1}the panel is running%f" >&2
    exit 1
}

if [[ -p $panel_fifo ]] { rm $panel_fifo }

mkfifo $panel_fifo

bspc control --subscribe > $panel_fifo &
xtitle -sf 'T%s' > $panel_fifo &
date '+S%I:%m %P' > $panel_fifo &

bspc config top_padding $panel_height

cat $panel_fifo | $HOME/etc/lemonbar/panel_bar | lemonbar -p \
    -n $panel_name \
    -g x$panel_height \
    -f $panel_font1 \
    -f $panel_font2 \
    -F $panel_fg \
    -B $panel_bg &

$HOME/etc/lemonbar/config
Code:

#!/usr/bin/zsh

typeset -gx panel_name=lemonbar panel_fifo=/tmp/panel-fifo panel_height=18 \
  panel_font1=ohsnap-9 panel_font2=ohsnap.icons-9

panel_fg='#ff839496'
panel_bg='#ff09090d'
panel_c1='#ff8c5760'
panel_c2='#ff7b8c58'
panel_c3='#ff8c6e43'
panel_c4='#ff58698c'
panel_c5='#ff7b5e7d'
panel_c6='#ff66808c'
panel_c7='#ff8c8b8b'
panel_c8='#ff545466'


joe_2000 12-13-2015 03:10 PM

I never scripted in zsh, but for sure it would help if you posted the actual errors that you are getting.

That said, why are you scripting in zsh in the first place? As far as I know, the main reason why people choose zsh over bash is that it has features that make its use as an interactive shell more convenient.

If you write scripts, this advantage goes away, so what's the point? You can expect bash to be available on most modern *nix systems. You certainly can't expect that for zsh. If anything, you want to script in sh to get even more portable scripts than in bash, but zsh... makes no sense to me.

I might be missing something, though...

like100ninjas 12-13-2015 03:16 PM

$ /home/satan/etc/lemonbar/panel_bar:35: parse error near `}'

i never really thought about it i guess. i started using zsh shortly after linux, and it never occurred to me to emulate bash all the time.

joe_2000 12-14-2015 07:27 AM

Quote:

Originally Posted by like100ninjas (Post 5463746)
i never really thought about it i guess. i started using zsh shortly after linux, and it never occurred to me to emulate bash all the time.

Hmm, not sure what you mean by "emulating" but if you are going to invest time and effort into learning a shell scripting language I'd highly recommend you to actually make an informed choice on which one you learn.

It's perfectly valid to use a different shell for scripting than for your interactive shell.

like100ninjas 12-14-2015 08:51 AM

yeah, it's pretty much just like bash. thanks for informing me. my hopes for anyone actually helping me weren't that high, anyway.

Habitual 12-14-2015 09:14 AM

Quote:

Originally Posted by like100ninjas (Post 5464038)
yeah, it's pretty much just like bash.

Yeah, No.
Until you need a bash'ism like
Code:

while [[ $(( read -r line ) 2>&1 ) -le 0 ]]
See
Code:

man bash
CONDITIONAL EXPRESSIONS

like100ninjas 12-14-2015 09:29 AM

the heck are you talking about? i don't really understand how that line is a bash'ism or what conditional expression would help. i tried some stuff out already. maybe you're saying that you know how to fix my problem, but it's so obvious that you're not telling. is that it? thanks for the bump, anyway.

grail 12-14-2015 11:23 AM

So I too am not much of a zsh guru, but are you sure about that while loop construct you created? I have never come across $(()) and had something in between the outer brackets?? What
error are you trying to redirect?

Habitual 12-14-2015 11:27 AM

I don't know zsh. Excuse me.

But this says a lot.
Zsh does not use readline, instead it uses its own and more powerful zle.

fu9ar 12-14-2015 11:52 PM

Look dude, he's saying that you should start by changing the first lines of your scripts to #!/bin/bash and then see what happens. It may also be beneficial to then post that output to the people helping you because then maybe some of the many people with the normal experience of using the standard tools available on every goddamn Linux system on the fucking planet can help you. Then, after ignoring their advice, you responded by getting progressively more huffy about it. If J. Hacker just fixes your shit, you learn nothing, which helps nobody.

berndbausch 12-15-2015 12:20 AM

Quote:

Originally Posted by fu9ar (Post 5464409)
Look dude, he's saying that you should start by changing the first lines of your scripts to #!/bin/bash and then see what happens.

Given all the curly parentheses in the program, most certainly bash will choke and hurl a bunch of error messages at the unfortunate programmer. To help, we need somebody with zsh skills. Sorry, that's not me.

fu9ar 12-15-2015 04:18 PM

That's my point. There are maybe like eight people in the universe writing zsh scripts. Ain't nobody got time for that.

The first step for you, like100ninjas, is to clean out the zsh and write normal bash scripts so that people can help you. You already know that subshells aren't working like you think they should. I've run into similar unexpected behavior with subshells in python scripts... So I rewrote them to work without using subshells and put a "figure out wtf subshells" task item on my long-term task list. I still haven't gotten to it because I've had better things to do. You are doing it wrong and you need to go back to the drawing board!

edit: you're right joe2k, I took it down a notch.

joe_2000 12-15-2015 04:34 PM

Hey Fu9ar, while I mostly agree with your point, I do think your wording is a bit harsh, and (according to my understanding) not in line with the LQ forum guidelines.
I did not perceive the OP's reaction as that moody, so let's just all step back and relax.

@like100ninjas: At this point I can see three options for you to solve your problem
- Use bash, try to fix your script as much as you can, and come back here for advice with the revised version of it when you are stuck. There are plenty of bash experts here who will be happy to help
- Wait for that zsh expert to see this thread, eventually
- Debug the script in zsh yourself by cutting it into little pieces. How can you make the script smaller and still reproduce the issue? If you can get it to be only one or two lines, and still fail, the problem will become more obvious.

My recommendation clearly is the first option, for reasons given in previous posts already.

Best of luck to you!

like100ninjas 12-15-2015 10:39 PM

was taking a break from this to configure other stuff...

@grail: eh, i don't know. i've tried making that line a bunch of different things. i know that's not the solution.

@fu9ar: i don't know what your problem is, but you need to take it somewhere else. i get huffy because people vomit on my post and try to sound smart while doing it.

@habitual: thanks. read is a built-in shell command, though, and i think the wiki is referring to the library.

fu9ar 12-16-2015 02:55 AM

The problem? You are arrogantly ignoring the advice given and vomiting a poorly written nonworking script and expecting people to read your mind about what the code is supposed to be doing.

What is the exit status of read -r line? Have you used help read to figure out what you are doing? Because from little that I can tell, this below is not what you are trying to do with it. What are you trying to do with it? Your incorrect and nonworking code also has no comments, so those of us reading it can't even begin to tell what you are trying to do! That's basic first year shit, man!


fu9ar@traveller:~$ help read
read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Read a line from the standard input and split it into fields.

Reads a single line from the standard input, or from file descriptor FD
if the -u option is supplied. The line is split into fields as with word
splitting, and the first word is assigned to the first NAME, the second
word to the second NAME, and so on, with any leftover words assigned to
the last NAME. Only the characters found in $IFS are recognized as word
delimiters.

If no NAMEs are supplied, the line read is stored in the REPLY variable.

Options:
(truncated output)


All times are GMT -5. The time now is 08:09 AM.