Tag Info

New answers tagged

0

Use this dd=$(date -d"$mm" +%Y%m%d) That's all. Example % mm="27 Jun 2011" % dd=$(date -d"$mm" +%Y%m%d) % echo $dd 20110627 to compare with your version % dd=$(date -d"27 Jun 2011" +%Y%m%d) % echo $dd 20110627 % dd=$(date -d'27 Jun 2011' +%Y%m%d) % echo $dd 20110627


4

One aspect of this problem isn't really about awk - and only a little bit about the shell. The problem is that on a standard, canonical tty most of the time the kernel's tty discipline is buffering your input - just echoing it to your screen and nowhere else - so that it can efficiently handle backspacing and such-like. However, when you press return or ...


5

I know you asked for a mv solution, however, despite the warning, this can be easily done with rename (in the Perl package): ~/tmp$ touch foo ~/tmp$ rename 's/$/\nbar/' foo Unsuccessful stat on filename containing newline at /usr/share/perl5/File/Rename.pm line 69. ~/tmp$ ls foo?bar


11

If you use bash, this command should work. mv a $'b\nc'


14

It is a bad idea (to have strange characters in file names) but you could do mv somefile.txt "foo bar" (you could also have done mv somefile.txt "$(printf "foo\nbar")" or mv somefile.txt foo$'\n'bar, etc... details are specific to your shell. I'm using zsh) Read more about globbing, e.g. glob(7). Details could be shell-specific. But understand that ...


1

man itself is a tool to format manual pages, not to browse them. The tool you use to browse the man pages is a pager. The default pager on most systems is less. In less, when you press / or ? to search, a few characters have a special meaning if you type them at the beginning of the search expression. The exclamation mark is one of them. To avoid ! having a ...


1

man -k \! Not sure that is what you are really asking?


1

SSH executes the remote command in a shell. It passes a string to the remote shell, not a list of arguments. The arguments that you pass to the ssh commands are concatenated with spaces in between. The arguments to ssh are sudo, usermod, -c, New Comment and user, so the remote shell sees the command sudo usermod -c New Comment user usermod parses Comment ...


3

for i in `cat servlist`;do echo $i;ssh $i 'sudo usermod -c "New Comment" user';done or for i in `cat servlist`;do echo $i;ssh $i "sudo usermod -c \"New Comment\" user";done


10

Use single quotes for the expression you used: sed 's/\//\\\//g' In double quotes, \ has a special meaning, so you have to backslash it: sed "s/\//\\\\\//g" But it's cleaner to change the delimiter: sed 's=/=\\/=g' sed "s=/=\\\/=g"


3

Try: sed 's/\//\\\//g' or using another delimiter to prevent you from escaping slash: sed 's,/,\\/,g'


2

Thanks Jeff Schaller for correcting my syntax error. I found a solution to my problem, this is working in Solaris 10. script: #!/bin/ksh HEX=30 DEC=`printf "%d\n" 0x${HEX}` ##Converted Hex to decimal echo "$DEC" OCT=$(printf '%o' $DEC) ##Converted decimal to octal echo "$OCT" ASCI=$(printf \\$OCT) ##Finally converted OCTAL to ASCII. echo "$ASCI" ...


2

You're mistakenly escaping the $ twice, leading printf to see printf \$( ... instead of (what I assume you want) of substituting the inside printf results. To that end, you could simplify that whole statement to: ASC=$(printf '%03o' $DEC)


1

When you use simple quotes, nothing is interpreted: ~$ echo 'foo:$var bar:$(base64 foo) something' foo:$var bar:$(base64 foo) something You have to unquote if you want the interpretation: ~$ echo 'foo:'$var' bar:'$(base64 foo)' something' foo:foo bar:Zm9vCg== something And since you don't know what it can contain, it's better to double quote: ~$ echo ...


3

That is because the process does not exist. The $$ is being evaluated locally, and all servers are being passed the same number. A number that is not a currently used PID on the servers. All the $ stuff is done by the shell, not the commands. You need to escape it, so that it is evaluated by the shell on the server. Try \$\$ e.g. for i in $(cat ...


2

Just for completeness, you dont need all those (") nor the final $(echo ...). Here's the simplified version of your assignments that produce the same effect: STARTIME=$(date +"%T") ENDTIME="$STARTIME today + 10 seconds" CALL="date -d '$ENDTIME' +'%H:%M:%S'" Note how you dont need to quote when doing var=$(...) but you do usually with var="many words": ...


0

Put single quotes inside your double-quoted ENDTIME variable, like so: ENDTIME="'$STARTIME today + 10 seconds'" CALL="$(echo date -d "$ENDTIME" +'%H:%M:%S')" It should give date -d '19:35:28 today + 10 seconds' +%H:%M:%S


1

You can edit your call statement as: CALL="$(echo date -d \"$ENDTIME\" +\'%H:%M:%S\')" The \ can be used to escape the characters " and '. Now echo $CALL will output as: date -d "22:46:37 today + 10 seconds" +'%H:%M:%S'


3

This matches ( and ) literally. () is used as capturing groups in Extended Regular Expressions (as used by r's grep() or the grep command with the -E option) or Perl Compatible Regular Expression which extend Extended Regular Expressions (as used by r's grep(..., perl=TRUE) or some grep commands with the -P option). Hence to match () literally, \ is used.


-2

By specifying the dot before the asterisk (.*) and the file name (newfile), you are telling grep to look for the pattern .\* in the file newfile, and the shell is expanding .\* to include the dot files in your directory. You probably want something like: cat newfile which will print the lines of your file.


4

Do an experiment! Run echo grep .* newfile What can you conclude from the result? How does the result change when you quote (place in single or double quotes) the first argument to grep? If you want the straight dope on this, read the POSIX spec for Pathname Expansion. Knowing everything about the various expansions will turn you into a shell guru in no ...


0

The shell is interpreting your *, not passing it to grep as a regex. To use a regex search with grep, you'll have to pass it an option telling it "this is a regex" - on my installation, it's -e. Try grep -e '.*' newfile.


7

FILEPATH_WITH_GLOB="/home/user/file_*" Now, FILEPATH_WITH_GLOB contains /home/user/file_* FILENAME=$(basename "$FILEPATH_WITH_GLOB") FILENAME contains file_*. echo $FILENAME #file_1234 $FILENAME being unquoted in list context, that expansion undergoes the split+glob operator, so that's expanded to the list of matching file: ...


3

When you deal with printing variable content, you should stick with printf instead of echo: printf 'visit:%s\n' "$site" will output visit: followed by content of $site and a newline regardless of characters in $site.


1

choroba is correct. Though if this were some sort of riddle, I'd respond with this: cat << EOF | sh > echo 'visit:$site' > EOF As jander points out this is wide open for an injection attack. It wasn't a serious answer, so if anyone was considering using something like this, don't use it with untrusted input. For example, validate the $site ...


3

You can't expand variables in single quotes. You can end single quotes and start double quotes, though: echo 'visit:"'"$site"'"' Or, you can backslash double quotes inside of double quotes: echo "visit:\"$site\""


0

I suggest to use echo -e or printf with \\n for new-line. Example (with echo -e): $ grep -C1 --group-separator="$(echo -e line1\\n\\nline2)" 'hello' a hello this is me line1 line2 something else hello hello bye Example (with printf): $ grep -C1 --group-separator="$(printf hello\\nfedorqui)" 'hello' a hello this is me hello fedorqui something else hello ...


2

Ooooh I found it, I just need to use the $'' syntax instead of $"": $ grep -C1 --group-separator=$'\n\n' 'hello' a hello this is me something else hello hello bye From man bash: QUOTING Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI ...


-1

This also can do curl -v "http://example.org/"'!'"287s87asdjh2/somepath/someresource"


0

To generate something to use as a command line argument, put it into a variable (or something else that you can ensure it gets into a word — then a single argument that gets passed to printf), then let bash/ksh93 do the rest. # Reading from stdin # pbpaste | bash -c ' printf echo\ %q\\n "$(cat)" # ' | pbcopy # Then paste it into your script and echo ...


6

For arbitrary content, you can also use: cat << 'EOF' > file RZW"a`4$k6[)b!^"'%*X6Evf RZW"a`4$k6[)b!^"'%*X6Evf EOF (as long as that content doesn't contain lines that consist of exactly EOF, in which case you can use a different delimiter). To include NUL characters (^@ usually entered with Ctrl+VCtrl+Space or Ctrl+VCtrl+@), the above will only ...


1

I would use a "here document". cat >my_file <<__EOF__ RZW"a4k6[)b!^"%*X6EvF __EOF__ When the shell sees the <<foo syntax, it remembers what foo is and continues reading until it sees foo on a line by itself. Then it passes everything it read (except for foo) to the program's standard input.


5

Just put it in single quotes: echo 'RZW"a4k6[)b!^"%*X6Evf' > file But if you have any single quotes in the string you need to escape each of those in double quotes ("'") and "glue" the result together like this: echo 'text without single quotes'"'"'and other text without single quote'


0

If you want to keep the same code as you currently have, I suggest using sed a little more to its capacity : i="`sed -n 's/'$'\r''//;s/,/ /g;s/values=//gp' /usr/local/app1/default.conf`" for data in $i; do cp -rvp /usr/local/dir1/$data.png /home/user1/dir1 done How it works : The -n flag to sed tells it to not print the lines automatically The ...


0

You probably edited that default.conf file on windows machine and copied it over. There seems to be an extra character at the end of that line before the newline. Try od -c default.conf. It probably gives you something like: 0000000 v a l u e s = w a d s s e a 0000020 d s e d a o k a w w ...


0

Actually I didn't get your use case, but all I understood is you want to find .png files from a directory and want to copy those files to another directory.If my understanding about your use case is correct then the below command helps, find /home/your/source/path/ -name \*.png -exec cp {} /home/your/destination/path/ \;


3

You don't need to escape the quotes inside a subshell, since the current shell doesn't interpret them (it doesn't interpret anything from $( to ), actually), and the subshell doesn't know about any quotes that are above. Quoting a subshell at variable assignment is unnecessary too, for more info see man bash.


0

@yaegashi had the approach right but my sed call seems to introduce the need for a third \. root@embedded:/data# set -vx root@embedded:/data# echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *${APP_PID} \+\([0-9]\+\):\([0-9]\+\).*/expr \1 \\\* 60 + \2/p")) echo Process Uptime: $(eval $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ ...


1

Embarrassingly, I seem to have failed to test as completely as I had thought. It turns out that modifying what I posted in my question to contain only two escapes (\\*) makes it work fine. root@embedded:~# $(/opt/bin/busybox ps -o pid,etime | sed -n "s/ *1155 \+\([0-9]\+\):\([0-9]\+\).*/expr \1 \\* 60 + \2/p") $(/opt/bin/busybox ps -o pid,etime | sed -n ...


1

assumping process is 16752 (which you seems to be able to figure out) expr $(date +%s) - $(stat -c %Y /proc/16752/environ ) where date +%s is current date in second since the epoch stat -c %Y /proc/16752/environ is "creation date" of /proc/16752/environ, that is the moment where proc #16752 was launched edit: maybe /proc/$PID/environ is the wrong ...


3

eval might help this case... ~ $ $(echo expr 1 \\* 2) + echo expr 1 \* 2 + expr 1 \* 2 expr: syntax error ~ $ eval $(echo expr 1 \\* 2) + echo expr 1 \* 2 + eval expr 1 \* 2 + expr 1 * 2 2 But it might be better to look up /proc/$pid/stat on Linux. pid=1155 hz=$(getconf CLK_TCK) uptime=$(awk '{print $1}' < /proc/uptime) starttime=$(awk '{print $22}' ...


0

From man xargs xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored. ...


1

You can use: find . -name '* *' -delete


6

I would avoid parsing ls output Why not : find . -regex '.* .*' -delete No problem with rm :-)


5

Look at this Suppose name "strange file" Solution one rm strange\ file solution two rm "strange file" solution three ls -i "strange file" you see the inode then find . -inum "numberoofinode" -exec rm {} \; In case of very strange file names like !-filename or --filename use rm ./'!-filename'


24

You can just use standard globbing on the rm command: rm -- *\ * This will delete any file whose name contains a space; the space is escaped so the shell doesn't interpret it as a separator. Adding -- will avoid problems with filenames starting with dashes (they won't be interpreted as arguments by rm). If you want to confirm each file before it's ...


0

I tried this as a comment but it came out all on one line [Harry@localhost]~% touch ">" [Harry@localhost]~% cat > ">" line 1 line 2 [Harry@localhost]~% cat ">" line 1 line 2 [Harry@localhost]~% ls -l ">" -rw-r--r-- 1 Harry Harry 14 Jun 5 12:04 > [Harry@localhost]~% rm ">" [Harry@localhost]~% ls -l ">" ls: cannot access >: No such ...


4

...aaand apparently rep doesn't transfer from one StackExchange site to another. Oh well, here's the comment I would have liked to have added to the accepted answer: While I generally agree with @l0b0's answer here, I suspect the placement of bare backticks in the "worst to best" list is at least partly a result of the assumption that $(...) is available ...


1

What I ended up doing initially also works: sudo sh -c "rm \>" This is, of course, a variant on the simpler sudo rm \>.


2

The problem was shell escaping. \ is special character in shell, and also in grep. In: grep export_cc\ =\ \$\{dir\}/aaa/bbb/ccc file export_cc\ =\ \$\{dir\}/aaa/bbb/ccc was interpreted by the shell to one string export_cc = ${dir}/aaa/bbb/ccc before passing to grep. You can use strace to check: $ strace grep export_cc\ =\ \$\{dir\}/aaa/bbb/ccc file ...



Top 50 recent answers are included