sh

Option not recognized

梦想的初衷 提交于 2019-12-17 17:12:10
问题 I'm trying to use a case statement to determine if I have a legal command. It looks something like this: function commandTest { case $1 in –score) echo "something";; *) echo "unknown";; esac } Now if I use the function like this, it doesn't work. case doesn't recognize the string correctly although it is identical. $ commandTest "-score" unknown What am I doing wrong here? 回答1: As posted, your sample code has an en-dash (Unicode U+2013) in front of score , instead of a minus sign (ASCII 0x2D)

/bin/sh: Odd string comparison error 'unexpected operator' [duplicate]

喜夏-厌秋 提交于 2019-12-17 16:53:28
问题 This question already has answers here : Unexpected operator error [duplicate] (4 answers) Closed 4 years ago . Found this error to be quite weird because previously my script was working and but after I moved it from the server I was working on to my local machine, it stopped working and just gave me an 'unexpected operator' error. # Else if the script is being run in the arrayscripts directory, add /output/ ... elif [ $basePath == "arrayscripts" ]; then echo "$dscr has started to run."

Escaping MYSQL command lines via Bash Scripting

孤人 提交于 2019-12-17 16:17:01
问题 PHP has mysql_real_escape_string() to correctly escape any characters that might cause problems. What is the best way to mimic this functionality for BASH? Is there anyway to do prepared mysql statements using bash? This seems to be the best way. Most of my variables won't (shouldn't) have special characters, however I give the user complete freedom for their password. It may include characters like ' and ". I may be doing multiple SQL statements so I'll want to make a script that takes in

IO Redirection - Swapping stdout and stderr

爱⌒轻易说出口 提交于 2019-12-17 15:53:40
问题 Given a shell script: #!/bin/sh echo "I'm stdout"; echo "I'm stderr" >&2; Is there a way to call that script such that only stderr would print out, when the last part of the command is 2>/dev/null, ie $ > sh myscript.sh SOME_OPTIONS_HERE 2>/dev/null I'm stderr Or, alternatively: $ > sh myscript.sh SOME_OPTIONS_HERE >/dev/null I'm stdout It's a question at the end of a set of lecture slides, but after nearly a day working at this, I'm nearly certain it's some sort of typo. Pivoting doesn't

Assigning the output of a command to a variable [duplicate]

女生的网名这么多〃 提交于 2019-12-17 15:29:41
问题 This question already has answers here : How to assign the output of a Bash command to a variable? [duplicate] (5 answers) Why does a space in a variable assignment give an error in Bash? [duplicate] (3 answers) Closed 26 days ago . I am new with unix and I am writing a shell script. When I run this line on the command prompt, it prints the total count of the number of processes which matches: ps -ef | awk '/siebsvc –s siebsrvr/ && !/awk/ { a++ } END { print a }' example, the output of the

Why does this snippet with a shebang #!/bin/sh and exec python inside 4 single quotes work?

我的未来我决定 提交于 2019-12-17 10:34:28
问题 I'm trying to understand one of the answers to this question: Cannot pass an argument to python with "#!/usr/bin/env python" #!/bin/sh ''''exec python -u -- "$0" ${1+"$@"} # ''' This works well, but I do not understand why it works with four ticks at the beginning of that line rather than three. In addition, why the hash near the end of that string? 回答1: Python supports triple-quoted strings: '''something''' Shell supports only single-quoted strings: 'something' By using four quotes, sh sees

Printing PDFs from Windows Command Line

为君一笑 提交于 2019-12-17 08:54:13
问题 I'm trying to print all pdfs in current dir. When I call this bash script in cmd ( singlepdf.sh ): '"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe"' /t Gemeinde_348_BioID_842_alt.pdf everything's working fine. When calling multiplepdfs.sh with this content: declare -a pdfs=(*.pdf) for pdf in ${pdfs[@]}; do echo -e "\nprinting **$pdf** with AcroRd32.exe...\n" '"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe"' /t $pdf sleep 3 done The echo shows that files are

How to get a password from a shell script without echoing

五迷三道 提交于 2019-12-17 05:16:28
问题 I have a script that automates a process that needs access to a password protected system. The system is accessed via a command-line program that accepts the user password as an argument. I would like to prompt the user to type in their password, assign it to a shell variable, and then use that variable to construct the command line of the accessing program (which will of course produce stream output that I will process). I am a reasonably competent shell programmer in Bourne/Bash, but I don

how to edit XML using bash script?

十年热恋 提交于 2019-12-17 05:14:08
问题 <root> <tag>1</tag> <tag1>2</tag1> </root> Need to change values 1 and 2 from bash 回答1: To change tag 's value to 2 and tag1 's value to 3 , using XMLStarlet: xmlstarlet ed \ -u '/root/tag' -v 2 \ -u '/root/tag1' -v 3 \ <old.xml >new.xml Using your sample input: xmlstarlet ed \ -u '/root/tag' -v 2 \ -u '/root/tag1' -v 3 \ <<<'<root><tag>1</tag><tag1>2</tag1></root>' ...emits as output: <?xml version="1.0"?> <root> <tag>2</tag> <tag1>3</tag1> </root> 回答2: You can use the xsltproc command (from

Calling shell functions with xargs

感情迁移 提交于 2019-12-17 03:26:36
问题 I am trying to use xargs to call a more complex function in parallel. #!/bin/bash echo_var(){ echo $1 return 0 } seq -f "n%04g" 1 100 |xargs -n 1 -P 10 -i echo_var {} exit 0 This returns the error xargs: echo_var: No such file or directory Any ideas on how I can use xargs to accomplish this, or any other solution(s) would be welcome. 回答1: Exporting the function should do it (untested): export -f echo_var seq -f "n%04g" 1 100 | xargs -n 1 -P 10 -I {} bash -c 'echo_var "$@"' _ {} You can use