sh

How can I parse long-form arguments in shell?

随声附和 提交于 2019-12-04 17:49:37
问题 Everything I see uses getopt or the slightly-fancier getopts which only supports one-character options (e.g., -h but not --help ). I want to do fancy long options. 回答1: I've done something like this: _setArgs(){ while [ "${1:-}" != "" ]; do case "$1" in "-c" | "--configFile") shift configFile=$1 ;; "-f" | "--forceUpdate") forceUpdate=true ;; "-r" | "--forceRetry") forceRetry=true ;; esac shift done } As you can see, this supports both the single-character and the longer options nicely. It

Need a shell script that deletes all files except *.pdf

白昼怎懂夜的黑 提交于 2019-12-04 13:32:46
问题 Can anyone write a shell script that deletes all the files in the folder except those with pdf extension? 回答1: This will include all subdirectories: find . -type f ! -iname '*.pdf' -delete This will act only in the current directory: find . -maxdepth 1 -type f ! -iname '*.pdf' -delete 回答2: $ ls -1 | grep -v '.pdf$' | xargs -I {} rm -i {} Or, if you are confident: $ ls -1 | grep -v '.pdf$' | xargs -I {} rm {} Or, the bulletproof version: $ find . -maxdepth 1 -type f ! -iname '*.pdf' -delete

1: command not found

爱⌒轻易说出口 提交于 2019-12-04 13:10:26
I'm writing a divides-by-three function in Bash, and it won't let me set a variable to a number. fizzy.sh: #!/usr/bin/env sh div3() { return `$1 % 3 -eq 0` } d=div3 1 echo $d Example: $ ./fizzy.sh ./fizzy.sh: line 7: 1: command not found Bash functions normally "return" values by printing them to standard output, where the caller can capture them using `func args ...` or $(func args ...) This makes functions work like external commands. The return statement, on the other hand, sets the value of $? . Normally that's going to be set to 0 for success, 1 for failure, or some other value for a

xcodebuild fails to build from terminal but succeeded from xcode

孤人 提交于 2019-12-04 07:46:15
I'm trying to build a framework using xcode 6-beta 3 when compiling it using xcode it works but when compiling it from a terminal with the command: xcodebuild -project <projName> -scheme <schemeName> -configuration Debug clean build I'm getting the following error CodeSign error: entitlements are required for product type 'Framework' in SDK 'Simulator - iOS 8.0'. Your Xcode installation may be damaged. and the build fails to complete. at the end of the log it says The following build commands failed: PhaseScriptExecution Run\ Script build/myProj.build/Debug-iphoneos/myScheme.build/Script

How to show wget progress bar only?

丶灬走出姿态 提交于 2019-12-04 07:42:19
问题 For example: wget http://somesite.com/TheFile.jpeg downloading: TheFile.tar.gz ... --09:30:42-- http://somesite.com/TheFile.jpeg => `/home/me/Downloads/TheFile.jpeg' Resolving somesite.co... xxx.xxx.xxx.xxx. Connecting to somesite.co|xxx.xxx.xxx.xxx|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1,614,820 (1.5M) [image/jpeg] 25% [======> ] 614,424 173.62K/s ETA 00:14 How can i get it to look like this downloading: TheFile.jpeg ... 25% [======> ] 614,424 173.62K/s ETA

How can I get FFmpeg to locate installed libraries when --sysroot is pointing to another directory?

依然范特西╮ 提交于 2019-12-04 07:22:32
I've been going at this, literally for days. I'm trying to build FFmpeg with libmp3lame for use in an Android application. The build script sets a --sysroot flag that points to the Android NDK directory necessary to build these libraries in a way that Android can use them. The problem comes when I add the flag to --enable-libmp3lame ; I get ERROR: libmp3lame >= 3.98.3 not found during the build start up. I know that LAME, and it's libraries are installed, because I can just run ./configure --enable-libmp3lame manually and the configuration launches without a hitch, and shows that libmp3lame is

How to Extract Parts of String in Shell Script into Variables

我只是一个虾纸丫 提交于 2019-12-04 06:15:59
问题 I am trying to do the following in sh. Here's my file: foo bar Tests run: 729, Failures: 0, Errors: 253, Skipped: 0 baz How can I pull the 4 numbers into 4 different variables? I've spent about an hour now on sed and awk man pages and I'm spinning my wheels. 回答1: Adopting my prior answer to use the heredoc approach suggested by @chepner: read run failures errors skipped <<EOF $(grep -E '^Tests run: ' <file.in | tr -d -C '[:digit:][:space:]') EOF echo "Tests run: $run" echo "Failures:

Using multiple sed commands

你说的曾经没有我的故事 提交于 2019-12-04 06:02:19
问题 Hi I'm looking to search through a file and output the values of a line that matches the following regex with the matching text removed, I don't need it output to a file. This is what I am currently using and it is outputting the required text but multiple times: #!/bin/sh for file in *; do sed -e 's/^owner //g;p;!d ; s/^admin //g;p;!d ; s/^loc //g;p;!d ; s/^ser //g;p;!d' $file done The preferred format would be something like this so I could have control over what happens inbetween: for file

In a bash script, what would $'\\0' evaluate to and why?

自古美人都是妖i 提交于 2019-12-04 05:52:02
In various bash scripts I have come across the following: $'\0' An example with some context: while read -r -d $'\0' line; do echo "${line}" done <<< "${some_variable}" What does $'\0' return as its value? Or, stated slightly differently, what does $'\0' evaluate to and why? It is possible that this has been answered elsewhere. I did search prior to posting but the limited number of characters or meaningful words in dollar-quote-slash-zero-quote makes it very hard to get results from stackoverflow search or google. So, if there are other duplicate questions, please allow some grace and link

Bash bug re $LINENO— or am I just confused?

做~自己de王妃 提交于 2019-12-04 04:45:50
Consider: #!/bin/bash echo ' ' $LINENO echo '' ' ' $LINENO The first echo correctly prints a 4, but the second echo prints a 5 instead of 6. Am I missing something, or is this a bug? (Using bash 3.00.15) It looks like an implementation misfeature (bug) in bash. I used: #!/bin/bash -p echo $LINENO echo ' ' $LINENO ' ' $LINENO ' ' $LINENO echo '' ' ' $LINENO which yielded: 2 3 3 3 6 Which supports the theory that the variable is evaluated before the shell considers the line to have been completed. Once the line has been completed, it updates the LINENO and proceeds. Bash versions tested: 3.2.48