Function in bash ignoring options when called through command substitution

Here is a script I have written for logging in bash called echoline.sh. When called with the flag -e, echoline prints to stderr instead of stdout. With the flag -o, the script writes to a given file.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#!/bin/bash
# ---------- echoline.sh ----------
to_stderr=false
### get input
set -f # stops * being expanded
cml_args="$*" ### flags passed via pipe are caught here
if test ! -t 0; then
read -r pipe_args # pipe
fi
### parse input
while getopts ":seco:" arg; do
case "${arg}" in
e)
cml_args=${cml_args//"-e"/}
;;
o)
outfile=$OPTARG
cml_args=${cml_args//"-o"/}
cml_args=${cml_args//"$outfile"/}
;;
*)
;;
esac
done
if [[ -z $outfile ]]; then
echo "Error in echoline.sh: outfile not given" 1>&2
exit 1
fi
### format data
if [[ -n "$pipe_args" ]]; then
indata="$pipe_args"
else
indata="$cml_args"
fi
### trim extra whitespace
indata=$( echo "$indata" | tr -s " " )
indata="${indata#" "}"
IFS='$'; arrIN=("$indata"); unset IFS;
### output to console
printable=$( IFS=$'n'; echo "${indata[*]}" )
if [[ $to_stderr == false ]]; then
echo "$printable"
else
echo "$printable" 1>&2
fi
### output to file
for (( x=0; x<${#arrIN[@]}; x++))
do
outdata=$( echo "${arrIN[x]}" | ./formatter.py | tr -d """ | tr -d "'" )
echo -e "$(date '+%H:%M:%S-%Z') > $outdata" >> "$outfile"
done
</code>
<code>#!/bin/bash # ---------- echoline.sh ---------- to_stderr=false ### get input set -f # stops * being expanded cml_args="$*" ### flags passed via pipe are caught here if test ! -t 0; then read -r pipe_args # pipe fi ### parse input while getopts ":seco:" arg; do case "${arg}" in e) cml_args=${cml_args//"-e"/} ;; o) outfile=$OPTARG cml_args=${cml_args//"-o"/} cml_args=${cml_args//"$outfile"/} ;; *) ;; esac done if [[ -z $outfile ]]; then echo "Error in echoline.sh: outfile not given" 1>&2 exit 1 fi ### format data if [[ -n "$pipe_args" ]]; then indata="$pipe_args" else indata="$cml_args" fi ### trim extra whitespace indata=$( echo "$indata" | tr -s " " ) indata="${indata#" "}" IFS='$'; arrIN=("$indata"); unset IFS; ### output to console printable=$( IFS=$'n'; echo "${indata[*]}" ) if [[ $to_stderr == false ]]; then echo "$printable" else echo "$printable" 1>&2 fi ### output to file for (( x=0; x<${#arrIN[@]}; x++)) do outdata=$( echo "${arrIN[x]}" | ./formatter.py | tr -d """ | tr -d "'" ) echo -e "$(date '+%H:%M:%S-%Z') > $outdata" >> "$outfile" done </code>
#!/bin/bash
# ---------- echoline.sh ---------- 


to_stderr=false

### get input
set -f # stops * being expanded
cml_args="$*" ### flags passed via pipe are caught here
if test ! -t 0; then
    read -r pipe_args # pipe
fi

### parse input
while getopts ":seco:" arg; do
    case "${arg}" in
        e)
            cml_args=${cml_args//"-e"/}
            ;;
        o)
            outfile=$OPTARG
            cml_args=${cml_args//"-o"/}
            cml_args=${cml_args//"$outfile"/}
            ;;
        *)
            ;;
    esac
done

if [[ -z $outfile ]]; then
    echo "Error in echoline.sh: outfile not given" 1>&2
    exit 1
fi

### format data
if [[ -n "$pipe_args" ]]; then
    indata="$pipe_args"
else
    indata="$cml_args"
fi
### trim extra whitespace
indata=$( echo "$indata" | tr -s " " )
indata="${indata#" "}"
IFS='$'; arrIN=("$indata"); unset IFS;

### output to console
printable=$( IFS=$'n'; echo "${indata[*]}" )
if [[ $to_stderr == false ]]; then
    echo "$printable"
else
    echo "$printable" 1>&2
fi

### output to file
for (( x=0; x<${#arrIN[@]}; x++))
do  
    outdata=$( echo "${arrIN[x]}" | ./formatter.py | tr -d """ | tr -d "'" ) 
    echo -e "$(date '+%H:%M:%S-%Z') > $outdata" >> "$outfile"
done

However, I am having an issue where if I have a script that uses echoline, in this case get_serial_number.sh, and that script is called via command substitution by another script, in this case first.sh, the -e flag passed to echoline is not picked up and prints to stdout instead. However, the script writes to the file just fine.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#!/bin/bash
# ---------- first.sh ----------
example_serial=$(./get_serial_number.sh)
echo "example_serial = $example_serial"
</code>
<code>#!/bin/bash # ---------- first.sh ---------- example_serial=$(./get_serial_number.sh) echo "example_serial = $example_serial" </code>
#!/bin/bash
# ---------- first.sh ---------- 


example_serial=$(./get_serial_number.sh)
echo "example_serial = $example_serial"

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#!/bin/bash
# ---------- get_serial_number.sh ----------
echoLine() {
here=$(pwd)
./echoline.sh -o "$here/test.log" "$@"
}
main() {
echoLine -e "This is supposed to write to stderr, but actually writes to stdout"
echo "This is supposed to write to stdout and does."
}
main
</code>
<code>#!/bin/bash # ---------- get_serial_number.sh ---------- echoLine() { here=$(pwd) ./echoline.sh -o "$here/test.log" "$@" } main() { echoLine -e "This is supposed to write to stderr, but actually writes to stdout" echo "This is supposed to write to stdout and does." } main </code>
#!/bin/bash
# ---------- get_serial_number.sh ---------- 

echoLine() {
    here=$(pwd)
    ./echoline.sh -o "$here/test.log" "$@"
}

main() {
    echoLine -e "This is supposed to write to stderr, but actually writes to stdout"
    echo "This is supposed to write to stdout and does."
}

main

Running first.sh gives this output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[root@DESKTOP-A8HSSI8 test]# ./first.sh
I can write to stderr here just fine!
example_serial = This is supposed to write to stderr, but actually writes to stdout
This is supposed to write to stdout and does.
</code>
<code>[root@DESKTOP-A8HSSI8 test]# ./first.sh I can write to stderr here just fine! example_serial = This is supposed to write to stderr, but actually writes to stdout This is supposed to write to stdout and does. </code>
[root@DESKTOP-A8HSSI8 test]# ./first.sh 
I can write to stderr here just fine!
example_serial = This is supposed to write to stderr, but actually writes to stdout
This is supposed to write to stdout and does.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật