sh

Using multiple sed commands

走远了吗. 提交于 2019-12-02 12:15:47
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 *; do sed 's/^owner //g;p' $file | head -1 sed 's/^admin //g;p' $file | head -1 sed '/^loc //g;p'

Not able to upload file to aws s3 using shell script

半城伤御伤魂 提交于 2019-12-02 11:31:34
问题 I am getting following error while trying to upload to s3. The script below seems to be correct but still I get above error. Please can someone help me in solving this error. My secret key and access ID are correct as I am able to connect to AWS using these keys in java and ruby. <?xml version="1.0" encoding="UTF-8"?> <Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

Trailing arguments with find -exec {} +

泄露秘密 提交于 2019-12-02 11:31:00
I want to add a trailing argument to the appending version of the -exec option of find . find . -exec echo {} asd + # expecting the following output: file1 file2 file3 [...] asd Does not work as {} must be the last word before + . (Bonus question: Why was that trivial looking feature not implemented?) What is the simplest expression to archive this, that can handle filenames with spaces and special characters? POSIX conformance would be a nice to have but it is sufficient if it works with Linux. find . -exec sh -c 'echo "$@" asd' _ {} + 来源: https://stackoverflow.com/questions/40429954/trailing

How to Extract Parts of String in Shell Script into Variables

孤街浪徒 提交于 2019-12-02 11:29:20
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. 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: $failures" echo "Errors: $errors" echo "Skipped: $skipped" Alternately (put this into a shell function to avoid

dos2unix command

前提是你 提交于 2019-12-02 10:33:18
问题 I have this script #!/bin/sh for i in `ls -R` do echo "Changing $i" fromdos $i done I want to remove "^M" charcaters from many files which are in more subdirectories. I got this: fromdos: Unable to access file Is there somethig i'm missing? Thanks in advance. 回答1: I guess you don't need a for loop. Here is a quick panorama of solutions for files with extension ".ext" (such commands shall be somehow restrictive) note : ^M is obtained with CTRL-V" + "CTRL-M" # PORTABLE SOLUTION find /home -type

Shell script — weird behaviour after concatenating string with variables

怎甘沉沦 提交于 2019-12-02 08:15:05
I am reading a .properties file from my shell script. I wanted to read some value for some key and after that want to append it in between some string but the output is weird. #!/bin/bash # Script used to read Property File FILE_NAME="Test.properties" prop_value=$(cat ${FILE_NAME} | grep Address) echo "ABC${prop_value}DEF" my Test.properties is like this Name=Pravin Age=25 Address=Mumbai asd=asd After executing this script I am expecting ABCAddress=MumbaiDEF but I am getting output like DEFAddress=Mumbai What would be the problem here? If I define any variable in a script it works, but when I

Shell script cut returning empty string

喜欢而已 提交于 2019-12-02 08:09:13
问题 I am writing a script that I am trying to get the linux distribution. I have the following code but it is returning an empty string. I am sure I am missing something OS=`cat /etc/os-release | grep -sw NAME` NEWOS=$OS | `cut -d \" -f2` echo $NEWOS 回答1: Like Honesty's answer says, you have two problems. Adding to his answer, I'd also like to address the use of backticks in your code. The command substitution can be done in two ways one is using $(command) and the other is `command` . Both work

sort on pipe-delimited fields not behaving as expected

大憨熊 提交于 2019-12-02 07:20:52
Consider this tiny text file: ab a If we run it through sort(1), we get a ab because of course a comes before ab . But now consider this file: ab|c a|c If we run it through sort -t'|' , we again expect a to sort before ab , but it does not! (Try it under your version of Unix and see.) What I think is happening here is that the -t option to sort is not really delimiting fields -- it may be changing the way (say) the start of field 2 would be found, but it's not changing the way field 1 ends . a|c sorts after ab|c because '|' comes after 'b' in ASCII. (It's as if the -t'|' argument is ignored,

Not able to upload file to aws s3 using shell script

时光毁灭记忆、已成空白 提交于 2019-12-02 07:18:28
I am getting following error while trying to upload to s3. The script below seems to be correct but still I get above error. Please can someone help me in solving this error. My secret key and access ID are correct as I am able to connect to AWS using these keys in java and ruby. <?xml version="1.0" encoding="UTF-8"?> <Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>AKIAJNODAIRHFUX3LHFQ</AWSAccessKeyId><StringToSign>PUT application/x-compressed-tar Sun, 20

Pass parameters that contain whitespaces via shell variable

ぃ、小莉子 提交于 2019-12-02 05:29:10
I've got a program that I want to call by passing parameters from a shell variable. Throughout this question, I am going to assume that it is given by #!/bin/sh echo $# i.e. that it prints out the number of arguments that are passed to it. Let's call it count-args . I call my program like this: X="arg1 arg2" count-args $X This works quite well. But now one of my arguments has a whitespace in it and I can't find a way to escape it, e.g. the following things do not work: X="Hello\ World" X="Hello\\ World" X="'Hello World'" In all of the cases, my program count-args prints out 2 . I want to find