sh

How to iterate over text file having multiple-words-per-line using shell script?

∥☆過路亽.° 提交于 2020-01-30 08:30:25
问题 I know how to iterate over lines of text when the text file has contents as below: abc pqr xyz However, what if the contents of my text file are as below, abc xyz cdf pqr lmn rst and I need to get values "abc" stored to one variable and"xyz" stored to another variable. How would I do that? 回答1: If the delimiter is a space then you can do: #!/bin/bash ALLVALUES=() while read line do ALLVALUES+=( $line ) done < "/path/to/your/file" So after, you can just reference an element by ${ALLVALUES[0]}

Is it possible with jq to use a deleted value in setting new ones?

三世轮回 提交于 2020-01-25 08:29:04
问题 I am using the bash to json parser jq Considering the following command: jq '. * .transitive | del(.transitive) | del(.scope,.scopedName)' package.json > package.github.json$$ And the following input : { "name": "navigation", "transitive": { "name": "navigation", "scope": "bs", "scopedName": "@bs/navigation" } } I am trying to get the following output : { "name": "@bs/navigation" } Is there a way before doing the delete of .scopedName , to use it's value to set .name ? 回答1: Transforming your

'sh' environment does not respect the PATH extensions, user's local PATH not in effect?

纵饮孤独 提交于 2020-01-25 06:42:14
问题 $ bl 1 $ sh -c 'bl 1' sh: bl: command not found The bl script is located in the user's PATH extension ( /home/user/.local/bin ) but the sh environment does not seem to be aware of it, the bash is. The main /usr/bin/sh executable symlinks to /usr/bin/bash . Placing a symlink in /usr/local/bin pointing to the local bl script does seem to fix the issue. Expanding the PATH manually $ PATH=/usr/bin:$HOME/.local/bin sh -c 'bl 1' also solves it, something I don't really understand since both the

Check Process using Shell Script

◇◆丶佛笑我妖孽 提交于 2020-01-25 03:18:28
问题 I am developing a Java program which checks the running processes, if not start that process. In my context I am executing the .sh file which is like this. #!/bin/sh echo "Hello World..." cnt=`ps -eaflc --sort stime | grep clientApplication.jar | grep -v grep | wc -l` if [ $cnt = 3 ] then echo "Services for Pigeon are already running..." else echo "Starting Services for Pigeon..." echo `java -jar clientApplication.jar` fi But it's not working. What's the problem? 回答1: Using the test

Bash Scripting and bc

旧时模样 提交于 2020-01-22 17:43:07
问题 I'm trying to write a bash script and I needed to do some floating point math. Basically I want to do something like this: NUM=$(echo "scale=25;$1/10" | bc) if [ $? -ne 0 ] then echo bad fi The problem I'm running into is $? tends to hold the output from the echo program and not the bc call. Is there a way I save the output from the bc program into a variable? EDIT: Thanks for the quick replies. Here's another way of looking at the problem. Say I modified the script a little bit so it looks

Getting user input after stdin has been redirected, in a bourne script

孤人 提交于 2020-01-22 15:33:09
问题 (this is indirectly a part of a much larger homework assignment) I have something like while read LINE do stuff-done-to-$LINE echo "Enter input:" read INPUT stuff-done-to-$INPUT done &lt infile I can't find a successful way of using the console/default stdin for the second read, instead of the redirected stdin. Needs to be pure bourne script. 回答1: I believe this is supported in the Bourne shell: exec 3<doc.txt while read LINE <&3 do stuff-done-to-$LINE # the next two lines could be replaced

Write an executable .sh file with Java for OSX

旧街凉风 提交于 2020-01-21 10:58:08
问题 So I am trying to write an .sh file that will be executable, this is how I'm currently writing it: Writer output = null; try { output = new BufferedWriter(new FileWriter(file2)); output.write(shellScriptContent); output.close(); } catch (IOException ex) { Logger.getLogger(PunchGUI.class.getName()).log(Level.SEVERE, null, ex); } So that writes the file just fine, but it is not executable. Is there a way to change the executable status when I write it? Edit: To further clarify, I am trying to

Write an executable .sh file with Java for OSX

会有一股神秘感。 提交于 2020-01-21 10:58:06
问题 So I am trying to write an .sh file that will be executable, this is how I'm currently writing it: Writer output = null; try { output = new BufferedWriter(new FileWriter(file2)); output.write(shellScriptContent); output.close(); } catch (IOException ex) { Logger.getLogger(PunchGUI.class.getName()).log(Level.SEVERE, null, ex); } So that writes the file just fine, but it is not executable. Is there a way to change the executable status when I write it? Edit: To further clarify, I am trying to

Randomly shuffling lines in Linux / Bash

那年仲夏 提交于 2020-01-18 21:36:48
问题 I have some files in linux. For example 2 and i need shuffling the files in one file. For example $cat file1 line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 and $cat file2 linea one linea two linea three linea four linea five linea six linea seven linea eight And later that i shuffling the two files i can obtain something like: linea eight line 4 linea five line 1 linea three line 8 linea seven line 5 linea two linea one line 2 linea four line 7 linea six line 1 line 6 回答1: You should

Randomly shuffling lines in Linux / Bash

情到浓时终转凉″ 提交于 2020-01-18 21:36:24
问题 I have some files in linux. For example 2 and i need shuffling the files in one file. For example $cat file1 line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 and $cat file2 linea one linea two linea three linea four linea five linea six linea seven linea eight And later that i shuffling the two files i can obtain something like: linea eight line 4 linea five line 1 linea three line 8 linea seven line 5 linea two linea one line 2 linea four line 7 linea six line 1 line 6 回答1: You should