sh

How to syntax check portable POSIX shell scripts? [duplicate]

微笑、不失礼 提交于 2019-12-09 06:30:16
问题 This question already has answers here : How do I syntax check a Bash script without running it? (8 answers) Closed 3 years ago . The following shell script executes well when provided /bin/true for the first argument, but may otherwise fail with a syntax error during execution! #!/bin/sh if $1 ; then exit; fi /tmp/asdf <<< ASDF # Something with syntax error in POSIX Surely some syntax errors (if not all?) can be avoided by static checking? How do I statically check whether a given Shell

Remove line breaks in Bourne Shell from variable

扶醉桌前 提交于 2019-12-09 05:11:03
问题 In bourne shell I have the following: VALUES=`some command that returns multiple line values` echo $VALUES Looks like: "ONE" "TWO" "THREE" "FOUR" I would like it to look like: "ONE" "TWO" "THREE" "FOUR" Can anyone help? 回答1: echo $VALUES | tr '\n' ' ' 回答2: Another method, if you want to not just print out your code but assign it to a variable, and not have a spurious space at the end: $ var=$(tail -1 /etc/passwd; tail -1 /etc/passwd) $ echo "$var" apache:x:48:48:Apache:/var/www:/sbin/nologin

should I avoid bash -c, sh -c, and other shells' equivalents in my shell scripts?

青春壹個敷衍的年華 提交于 2019-12-09 01:38:15
问题 Consider the following code: #!/bin/bash -x VAR='1 2 3' bash -c "echo "\$$VAR"" eval "echo "\$$VAR"" bash -c "echo \"\$$VAR\"" eval "echo \"\$$VAR\"" Which outputs: + VAR='1 2 3' + bash -c 'echo $1' 2 3 3 + eval 'echo $1' 2 3 ++ echo 2 3 2 3 + bash -c 'echo "$1 2 3"' 2 3 + eval 'echo "$1 2 3"' ++ echo ' 2 3' 2 3 It seems both eval and bash -c interprets the codes the same way i.e "echo "\$$VAR"" to 'echo $1' 2 3 and "echo \"\$$VAR\"" to 'echo "$1 2 3"' . The only difference I seem to notice

How to check if two paths are equal in Bash?

橙三吉。 提交于 2019-12-09 00:28:19
问题 What's the best way to check if two paths are equal in Bash? For example, given the directory structure ~/ Desktop/ Downloads/ (symlink to ~/Downloads) Downloads/ photo.png and assuming that the current directory is the home directory, all of the following would be equivalent: ./ and ~ ~/Desktop and /home/you/Desktop ./Downloads and ~/Desktop/Downloads ./Downloads/photo.png and ~/Downloads/photo.png Is there a native Bash way to do this? 回答1: Bash's test commands have a -ef operator for this

feedback stdin and stdout of two processes

风流意气都作罢 提交于 2019-12-08 19:36:23
问题 I have two processes that interface with one another over stdin and stdout. Suppose I have process A and process B. The stdout of B needs to feed into the stdin of A, and the stdout of A needs to feed in to that of process B. Is there a simple way to express this relationship in a simple command, or is there a basic shell script that can enable this? Thanks in advance. 回答1: Take a look at named pipes. Create one pipe for A to B, and one pipe for B to A. Then start A with its stdout redirected

popen fails with “sh: <command>: not found”

a 夏天 提交于 2019-12-08 18:01:36
问题 I'm developing a server application and I recently encountered this wierd error on a testing server (Debian Squeeze). Every executable I pass to popen fails with a msg: sh: sort: not found // happens to any command This happens regardless whether I point to the full path returned by "type" or keep it short . As mentioned earlier, this happens at only one testing environment, to add confusion, am running the same OS and had no problem whatsoever. Popen is apparently using sh to execute

Multiline assignment in bash

懵懂的女人 提交于 2019-12-08 16:54:17
问题 In .cmd files on windows I do: SET JARS=^ ./lib/apache-mime4j-0.6.jar;^ ./lib/apache-mime4j-0.6.jar;^ ./lib/bsh-1.3.0.jar;^ ./lib/cglib-nodep-2.1_3.jar;^ ./lib/commons-codec-1.6.jar;^ ./lib/commons-collections-3.2.1.jar;^ ./lib/commons-exec-1.1.jar;^ ./lib/commons-io-2.0.1.jar;^ ./lib/commons-io-2.3.jar; How can I do such multiline assignment in shell? 回答1: The question implicitly requests single line output , as I will show. test.bat @SET JARS=^ ./lib/apache-mime4j-0.6.jar;^ ./lib/apache

Ignore errors when running npm scripts sequentially

﹥>﹥吖頭↗ 提交于 2019-12-08 15:44:41
问题 Whenever I build my project to be served, I have to execute three scripts: npm run build:local //calls tsc to compile ts into js files npm run build:webpack //invokes webpack to bundle js files into one minified file npm run serve //runs my lite-server to preview files I wanted to run these commands sequentially as: npm run build:local && npm run build:webpack && npm run serve However, due to needing to have some JQuery in my ts files, I get an error during npm run build:local that throws

Why are all NUL removed from my script?

╄→尐↘猪︶ㄣ 提交于 2019-12-08 15:38:04
问题 It seems like bash, and also dash, filter out any ASCII NUL from my scripts. $ printf 'test="\000a" ; echo ${#test}' | sh 1 $ printf 'test="\001a" ; echo ${#test}' | sh 2 $ printf 'ec\000ho test' | sh test $ # (Same for bash) While I agree that using NUL is a bad bad idea (for example argument passing to programs works with NUL-terminated strings), I don't see where this behaviour is sanctioned by the POSIX standard. It gets even worse when this behaviour is deciding on the syntactical

Using mkdir -m -p and chown together correctly

我的梦境 提交于 2019-12-08 15:15:17
问题 I would like to create a directory using a bash script and then set the mode to 00755 at the same time mkdir -p -m=00755 "/dir/dir2" Is this the correct way of using them together and can I also add chown command to the same line while creating them? 回答1: It goes a little like this: install -d -m 0755 -o someuser -g somegroup /dir/dir2 回答2: If you want to set the owner during creation, you can simply impersonate as this user, using sudo for example: sudo -uTHE_USER mkdir -p -m=00755 "/dir