sh

how to replace “/” on path string with “\\/” using sed?

扶醉桌前 提交于 2019-12-01 18:37:01
I've tried this: PATH="/user/dj/a/mydir" PATH_FORMAT=`echo "${PATH}" | sed 's/\//\\\//'` but it only replace the first "/". I want the resulting PATH_FORMAT value to be: "\/user\/dj\/a\/mydir" How can I do that? Add a g flag to your substitute command: echo "${PATH}" | sed 's/\//\\//g' Or more readable, as per Jonathan Wakely in the comments: echo "${PATH}" | sed 's:/:\\/:g' To achieve what you describe in your question, you need one more backslash: echo /user/dj/a/mydir | sed 's:/:\\\/:g' Output: \/user\/dj\/a\/mydir Use Shell's parameter expansion, if you are running a shell script: MYPATH="

Find out if file has been modified within the last 2 minutes

我的未来我决定 提交于 2019-12-01 15:04:10
In a bash script I want to check if a file has been changed within the last 2 minutes. I already found out that I can access the date of the last modification with stat file.ext -c %y . How can I check if this date is older than two minutes? I think this would be helpful, find . -mmin -2 -type f -print also, find / -fstype local -mmin -2 Aubrey Kilian Complete script to do what you're after: #!/bin/sh # Input file FILE=/tmp/test.txt # How many seconds before file is deemed "older" OLDTIME=120 # Get current and file times CURTIME=$(date +%s) FILETIME=$(stat $FILE -c %Y) TIMEDIFF=$(expr $CURTIME

Why will script work with /bin/bash but not /bin/sh?

笑着哭i 提交于 2019-12-01 13:28:51
问题 I am trying to understand why the script will work with #!/bin/bash but not #!/bin/sh . I am running Cygwin and both sh.exe and bash.exe seem to be identical (same file size). $ cat 1.sh #!/bin/sh while read line; do echo ${line:0:9} done < <(help | head -5) $ ./1.sh ./1.sh: line 4: syntax error near unexpected token `<' ./1.sh: line 4: `done < <(help | head -5)' $ cat 2.sh #!/bin/bash while read line; do echo ${line:0:9} done < <(help | head -5) $ ./2.sh GNU bash, These she Type `hel Use

Script to get user that has process with most memory usage?

断了今生、忘了曾经 提交于 2019-12-01 13:02:37
问题 How can I write a script that gives an output of the user that has the process with the most memory usage in the system. The script is sh. I tried to use top command as the starting point but it seems it does not work with pipes because it continues running until it is quit. 回答1: If you just want the user name of the process using the most memory, try something like: $ ps axho user --sort -rss | head -1 This checks the resident memory size rss of the processes. If you'd rather check the whole

Want value of $bhai in javascript

末鹿安然 提交于 2019-12-01 12:12:54
#!/bin/sh echo "Content-type: text/html" echo "" echo '<html>' echo '<head>' echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' echo '</head>' echo '<body><center><br><h3 align='center'>STATUS</h3></br></center>' list=$(ls -l /tmp | grep "^d" | awk -F" " '{print $9}') list1=$(echo $list | wc -w) i=1 while [ $i -le $list1 ] do bhai=$(echo $list | cut -d' ' -f$i) echo '<a href="#" onclick="myFunction(); return false;" id="movie" style="font-size: 30px; text-decoration: none; margin-left: 1cm">'$bhai'</a></font><br>' i=$((i+1)) done echo ' <script type="text/javascript">

Cocoa run AppleScript that already contains double quotes

笑着哭i 提交于 2019-12-01 12:11:20
I have a Cocoa application that need admin permission to execute a cmd, but in some extreme case like filename containing double quote, this code does not work. NSString *fileName = @"~/Documents/My\" File/"; NSString *cmd = [NSString stringWithFormat:@"chown -R '%@' '%@';NSUserName(), fileName]; NSString *cmd_execute = [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges",cmd_execute]; //This line is the problem, our %@ contains double quote, so cocoa //cannot interpret correctly. Here is the correct way to pass arguments into an AppleScript handler using

get parent directory of a file in bash

亡梦爱人 提交于 2019-12-01 11:36:18
How do I get a parent directory for a file? I want it to be safe on all kind of names: . .. path/to/my/file /absolute/path/to/my/file '-rf --no-preserve-root whatever'/test.zip (symbolic links) `'"`'{(}) I am more interested getting the canonical location on the file system than in traversing the path stated in the filename. Note that there are similar questions to this one, but none of them focuses on correctness, relative/absolute paths and "unsafe" names: [1] bash get the parent directory of current directory [2] Retrieve parent directory of script [3] bash filepath to parent directory of

script doesn't run while executing in clearcase

别等时光非礼了梦想. 提交于 2019-12-01 10:52:31
I am trying to execute the following build script and it is returning no error but it is not executing the script inside it. there is a view tag with the following name. it can be seen with cleartool lsview <view-tag> . I can do cleartool setview <view-tag> but it doesn't run the sh /abc/cds/fg/bin/ant -t all. CLEARCASE_VIEWNAME=NYC_CYN cleartool setview -exec "newgrp orange; cd /abc/cds/fg/bin; sh /abc/cds/fg/bin/ant -t all -i ' '" $CLEARCASE_VIEWNAME Thanks for any help !! VonC First, don't use setview . It triggers a sub-shell, which doesn't play well with scripts. See " Python and

printf field width doesn't support multibyte characters?

与世无争的帅哥 提交于 2019-12-01 10:31:25
I want printf to recognize multi-byte characters when calculating the field width so that columns line up properly... I can't find an answer to this problem and was wondering if anyone here had any suggestions, or maybe a function/script that takes care of this problem. Here's a quick and dirty example: printf "## %5s %5s %5s ##\n## %5s %5s %5s ##\n" '' '*' '' '' "•" '' >## * ## >## • ## Obviously, I want the result: >## * ## >## • ## Any way to achieve this? The best I can think of is: function formatwidth { local STR=$1; shift local WIDTH=$1; shift local BYTEWIDTH=$( echo -n "$STR" | wc -c )

Getting an error EOF : Command no found when using ssh

依然范特西╮ 提交于 2019-12-01 10:23:30
问题 I am trying to rename the filenames in remote server like filename-dirname.suffix and copy the files to my server . I had written code like .... #!/usr/bin/bash TRANSFERSERVERXMLS="/emp/transfer/XMLS" REMOTESERVERXMLS="remoteemp/empdir/XMLS" # renaming the filenames in remote server like filename-dirname.suffix ssh abc@xyz REMOTESERVERXMLS=$REMOTESERVERXMLS 'bash -s'<< 'EOF' for i in $REMOTESERVERXMLS/* ; do if [[ -d $i ]]; then dirname=$(basename $i) for j in $REMOTESERVERXMLS/$dirname/* ;