dash-shell

How to iterate over the characters of a string in a POSIX shell script?

强颜欢笑 提交于 2019-12-12 12:23:19
问题 A POSIX compliant shell shall provide mechanisms like this to iterate over collections of strings: for x in $(seq 1 5); do echo $x done But, how do I iterate over each character of a word? 回答1: It's a little circuitous, but I think this'll work in any posix-compliant shell. I've tried it in dash , but I don't have busybox handy to test with. var='ab * cd' tmp="$var" # The loop will consume the variable, so make a temp copy first while [ -n "$tmp" ]; do rest="${tmp#?}" # All but the first

“echo” output different answer by sh and bash

回眸只為那壹抹淺笑 提交于 2019-12-11 07:59:12
问题 I ran my script using three ways and the output was different, could you explain to me why it works like that? Thanks!! Here is my script #!/bin/bash #Program: # This program shows "Hello World!" in your screen. echo -e "Hello World! \a\n" exit 0 And if i run it by bash and ./ like bash sh01.sh the output is Hello World! However, if i use sh like sh sh01.sh it would be like -e Hello World! And Here is some other information OS: Ubuntu 16.04.3 type sh -> dash 回答1: echo is not very portable

Bash vs. Dash behavior with the command `echo -ne “hello\n”`

你说的曾经没有我的故事 提交于 2019-12-10 14:23:55
问题 I got different behaviors with the same command echo -ne "hello\n" with bash and with dash. See below : $ bash -c 'echo -ne "hello\n"' hello $ dash -c 'echo -ne "hello\n"' -ne hello Why is that ? I don't understand at all… My system : $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 12.04.5 LTS Release: 12.04 Codename: precise 回答1: The POSIX specification for echo doesn't support any arguments. See it here. And while the spec mentions -n it does so to

List currently defined functions in dash?

梦想的初衷 提交于 2019-12-05 22:16:52
I'd like to list the currently defined functions in dash . Is there any way of doing that? The closest I've been able to come up with is type which can be used to test if a function exist, but other than that I'm stumped. P.S. I'm talking about dash here (not bash or zsh ). Looking at exec.c it seems that no, there is none - the table is static, there's no such functionality in the file and none of the exported functions (unsetfunc etc) appear to offer the possibility of iterating, so unless I missed something I'd say you'll need to write a patch :) 来源: https://stackoverflow.com/questions

Run bash script with sh

别等时光非礼了梦想. 提交于 2019-12-02 22:54:31
I have bash script and it requires bash. Another person try to run it with sh script_name.sh And it fails because sh is symbolic link to dash in his distribution. $ ls -la /bin/sh lrwxrwxrwx 1 root root 4 Aug 25 16:06 /bin/sh -> dash I have an idea to use wrapper script: #!/bin/sh bash script_name.sh The goal is to run .sh script by sh with bash in system having symbolic link to dash. Well, usually you use the shebang to tell the shell to use the correct interpreter: #!/bin/bash # your script here You have to set the script to be executable: chmod +x my_script.sh And let the user start it with

Regression: Exported Bash function lost after going through another process

こ雲淡風輕ζ 提交于 2019-12-01 07:35:50
When moving from Ubuntu 14.04 to 16.04, I've noticed several of my Bash scripts failing due to missing exported functions. I wonder whether this is related to the fixes for the Shellshock bug , even though I simply export -f the functions, and not relying on the Bash-internal function representation. The failure does not occur in a direct Bash subshell, only if there's another process in between. For example, Bash invoking awk / Perl / Vim invoking another Bash. Here's an example with Perl: Good $ foo() { echo "foobar"; } $ export -f foo $ export -f; foo foo () { echo "foobar" } declare -fx

Regression: Exported Bash function lost after going through another process

女生的网名这么多〃 提交于 2019-12-01 04:21:31
问题 When moving from Ubuntu 14.04 to 16.04, I've noticed several of my Bash scripts failing due to missing exported functions. I wonder whether this is related to the fixes for the Shellshock bug, even though I simply export -f the functions, and not relying on the Bash-internal function representation. The failure does not occur in a direct Bash subshell, only if there's another process in between. For example, Bash invoking awk / Perl / Vim invoking another Bash. Here's an example with Perl:

Does `dash` support `bash` style arrays?

懵懂的女人 提交于 2019-11-30 23:34:36
In the dash shell environment I am looking to split a string into arrays. The following code works in bash but not in dash . IFS="" var="this is a test|second test|the quick brown fox jumped over the lazy dog" IFS="|" test=( $var ) echo ${test[0]} echo ${test[1]} echo ${test[2]} My Question Does dash support arrays in this style. If not are there any suggestions for parsing this out into an another type of variable without the use of a loop? dash does not support arrays. You could try something like this: var="this is a test|second test|the quick brown fox jumped over the lazy dog" oldIFS=$IFS

Does `dash` support `bash` style arrays?

☆樱花仙子☆ 提交于 2019-11-30 17:35:37
问题 In the dash shell environment I am looking to split a string into arrays. The following code works in bash but not in dash . IFS="" var="this is a test|second test|the quick brown fox jumped over the lazy dog" IFS="|" test=( $var ) echo ${test[0]} echo ${test[1]} echo ${test[2]} My Question Does dash support arrays in this style. If not are there any suggestions for parsing this out into an another type of variable without the use of a loop? 回答1: dash does not support arrays. You could try

Shell scripting input redirection oddities

十年热恋 提交于 2019-11-30 13:43:12
问题 Can anyone explain this behavior? Running: #!/bin/sh echo "hello world" | read var1 var2 echo $var1 echo $var2 results in nothing being ouput, while: #!/bin/sh echo "hello world" > test.file read var1 var2 < test.file echo $var1 echo $var2 produces the expected output: hello world Shouldn't the pipe do in one step what the redirection to test.file did in the second example? I tried the same code with both the dash and bash shells and got the same behavior from both of them. 回答1: A recent