sh

Need counter on find and xarg combo

て烟熏妆下的殇ゞ 提交于 2019-12-24 17:23:25
问题 So I have this code: find cobacoba -type f | xargs -n 5 bash -c 'a=(${0} ${1} ${2} ${3} ${4}); echo "File #: ${a[*]}";' Hoping Result: File #: cobacoba/1.3 cobacoba/1.6 cobacoba/1.q cobacoba/1.5 File #: cobacoba/1.1 cobacoba/1.q2 cobacoba/1.q23 cobacoba/1.4 File #: cobacoba/1.2 I would like to replace # with counter, like 1, 2, 3, so on... 回答1: You can postprocess your output with awk to replace # with the line number: find cobacoba -type f | xargs -n 5 bash -c 'a=(${0} ${1} ${2} ${3} ${4});

How to speed up find for listing git repositories?

妖精的绣舞 提交于 2019-12-24 17:19:14
问题 I want to find all git repositories lying in some directory, but not its subdirectories, say ~/repo . Two simple approaches are find ~/repo -depth 2 -type d -name '.git' | while read repo … or for repo in ~/repo/*/.git … The version using find is magnitudes slower than the one with the globbing pattern. I am very surprised by this, because there is no real reason why one method would need more system calls than the other to gather its informations. I tried a smarter version of the find

How do I redirect stdout from one process to be read as file descriptor 3 in another?

99封情书 提交于 2019-12-24 13:06:59
问题 I want to redirect (stdout,stderr) from one process to (stdin,file descriptor 3) of another. How do I do that? For example, if I have myscript.sh : #!/bin/sh # myscript.sh echo "stdout" echo "stderr" >&2 And reader.sh : #!/bin/sh # reader.sh read key echo "stdin: $key" read key <&3 echo "fd3: $key" How do I pipe the output of myscript.sh to reader.sh such that the final output is: stdin: stdout fd3: stderr The closest I've gotten is: ./myscript.sh 2>&3 | ./reader.sh But that hangs waiting for

Functions in sh shell

不打扰是莪最后的温柔 提交于 2019-12-24 11:46:09
问题 Do functions not exist in the sh shell? I am trying to convert a bash script to also run in sh and I'm having trouble dumbing down the code. Without functions and arrays, its become difficult to replicate the scripts functionality. Ideally I want to write one script that will run in either shell, not two slightly similar scripts. 回答1: There are functions in sh , but the function keyword is missing. Define your functions as frobnicate () { # .... } With arrays, you are out of luck. 来源: https:/

Validate language code for Wikimedia languages [closed]

*爱你&永不变心* 提交于 2019-12-24 10:47:37
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I have a shell script that uses Wikidata Query Service (WDQS) to get required data. The SPARQL query that run WDQS takes input parameter language code. Is there a way that I can check in shell script if the input language code is a valid Wikimedia language code as the first column data in below link https://www

recover files deleted with rm command

坚强是说给别人听的谎言 提交于 2019-12-24 08:26:19
问题 Please if I run the command # CREATE TRASH FOLDER if ! [ -d "$HOME/deleted" ] ; then mkdir $HOME/deleted fi TRASH=$HOME/deleted mv $@ $TRASH To move file or directory to the trash created. what is the possible command i can run to recover same file to the original directory 回答1: If you create a deleted directory like this, you will probably get some unexpected behavior. For example: rm_script test.txt cd ../other_directory rm_script test.txt will create a single file test.txt with the content

In Linux shell scripts what does 'x=${1:3:1}' mean?

匆匆过客 提交于 2019-12-24 08:16:11
问题 Inside the function of a shell script I see something like this func() { local x x=${1:3:1} ... } What does x=${1:3:1} mean? I know that $1 , $2 and $3 are arguments of the function. So does the above statement mean that x = $1:$2:$3 ? Also, it is really helpful if someone can suggest on how do I google search for speacial characters like this? Any standard keywords? I tried searching 'what is ":" in shell scripts' etc.. But the results are random when trying to search for special characters.

Creating two files from one file using unix

送分小仙女□ 提交于 2019-12-24 07:48:43
问题 I have one input file, I need to read and apply some condition and route to two other files. 100 COMPANY Records 500ABC COMPANY 345 2pen9999out 2cow7777out 2goa7777out 500ABC COMPANY IN 456 2car9999out 2cow7777out 2HAT7777out 2BAL9999out 2BAL6666out here, record start with 5 was header and 2 was detail i need to create two file ABC_COMPANY.txt and ABC_COMPANY_IN.txt ? I have written below logic, which I want to convert using awk or other right approach? NUMBER1="666" NUMBER2="777" while read

Avoid matching . and .. when looping through files in POSIX shell

匆匆过客 提交于 2019-12-24 07:47:02
问题 I want to do something to every file in a directory, so I have for f in /path/* /path/.*; do [ -e "$f" ] || continue do_thing "$f" done Unfortunately, this also matches . and .. , which is undesirable in this case. How can I avoid matching . and .. ? 回答1: To run do_thing on every file in the current directory, try: find /path -maxdepth 1 -type f -exec do_thing {} \; This method avoids looping and avoids parsing ls . How it works find /path This starts a find command and tells find to look in

Shell Script: Read line in file

删除回忆录丶 提交于 2019-12-24 05:03:03
问题 I have a file paths.txt : /my/path/Origin/.:your/path/Destiny/. /my/path/Origin2/.:your/path/Destiny2/. /... /... I need a Script CopyPaste.sh using file paths.txt to copy all files in OriginX to DestinyX Something like that: #!/bin/sh while read line do var= $line | cut --d=":" -f1 car= $line | cut --d=":" -f2 cp -r var car done < "paths.txt" 回答1: Use translate : tr command & apply cp command in the same go! #!/bin/sh while read line; do cp `echo $line | tr ':' ' '` done < "paths.txt" 回答2: