I am using Gammu for receiving SMS. Once received I am sending them as emails. Yet if sender ID of the SMS contains space, my script is unable to process a file. I.e.: If the file name is IN20210409_104439_00_SOME NAME_00.txt runonreceive script uses $1 to handle the filename but it saves only IN20210409_104439_00_SOME and obviously such file doesn't exist which brings errors. runonreceive script: sed -i '1 i\Subject: Incoming sms\n\n' /var/spool/gammu/inbox/$1 cat /var/spool/gammu/inbox/$1 | msmtp desrired@email.com  So I am looking for a solution for handling full filename (with spaces) or to rename the file in that script (on-the-fly) and send out renamed SMS as email.
1847
Or, an introductory guide to robust filename handling and other string passing in shell scripts.  I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names).  I encountered a problem such as the following:   I have a file name containing a space hello world, and it was treated as two separate files hello and world. I have an input line with two consecutive spaces and they shrank to one in the input. Leading and trailing whitespace disappears from input lines. Sometimes, when the input contains one of the characters \[*?, they are replaced by some text which is is actually the name of files. There is an apostrophe ' (or a double quote ") in the input and things got weird after that point. There is a backslash in the input (or: I am using Cygwin and some of my file names have Windows-style \ separators).   What is going on and how do I fix it?
44876
I have a situation where I have the following:  file01.ext . . . file99.ext file100.ext . . .   And I want to (using rename ) insert the '0' for the files 01-99 (so that they become 000-099) but not modify files larger than 100. Which expression should I use and what is a good source on learning regular expressions such as these?   On Windows I would have used Total Commander's multi rename function which has a very simple renaming syntax. Are there some good alternatives for that tool (multi-rename) for Ubuntu?
27803
How can I determine the name of the Bash script file inside the script itself?  Like if my script is in file runme.sh, then how would I make it to display "You are running runme.sh" message without hardcoding that?
68162
doesn't seem to work  The next example would delete everything between "ONE" and "TWO:"   #!/bin/sh sed ' /ONE/ { # append a line     N # search for TWO on the second line      /\n.*TWO/ { # found it - now edit making one line         s/ONE.*\n.*TWO/ONE TWO/     } }' file   i was expecting to delete everything between ONE and TWO.i tried with file where file contains.  [user5@mailserver ~]$ cat file ONE hello TWO   but the output come like   [user5@mailserver ~]$ ./test ONE hello TWO   I was expecting ONE TWO I am trying to make it correct but not able to do.
44359
I want to include a batch file rename functionality in my application. A user can type a destination filename pattern and (after replacing some wildcards in the pattern) I need to check if it's going to be a legal filename under Windows. I've tried to use regular expression like [a-zA-Z0-9_]+ but it doesn't include many national-specific characters from various languages (e.g. umlauts and so on). What is the best way to do such a check?
14911
I am only using the 128 character set defined in the original ANSI standard.  But as a whole how are the files implmeneted differently.    I am not concerned with the display, i.e. if a tab is displayed with 6 or 8 characters but the actual internal representation in memory  One difference I've heard is the use of \r\n (Windows) vs. \n for line termination (Linux).
104802
Normally to remove files with spaces in their filename you would have to run:  $ rm "file name"   but if I want to remove multiple files, e.g.:  $ find . -name "*.txt" | xargs rm   This will not delete files with spaces in them.
46580
I'm trying to escape the space char for a path in Bash, but neither using a  or  works.  .sh script:  ROOT="/home/hogar/Documents/files/" FILE=${ROOT}"bdd.encrypted" DESTINATION="/home/hogar/Ubuntu\ One/folder"  mv ${FILE} ${DESTINATION}   After execute the script (./file) this is the result:  mv: target 'One/folder' is not a directory   Why does the mv command split the string, and how do I stop this from happening?
75977
Here's a similar question but I ran into a problem that was not addressed in the question.   I want to do something similar, however my file is tab delimited instead of space delimited.  I want to change field 3. Lets say my file looks like this:  NAME\tNUMBER\tID\tSEQ\t...\t...\t...    Lets say I want to change field 3 to AD  awk '{$3 = "AD"; print}' infile &gt; outfile   my output is  NAME\tNUMBER\tAD SEQ ... ... ... ... ... ...   my tabs turn into spaces after the replaced  I'm sure I can just tr the output file but I want to know what causes awk to change the field delimiter.  expected output:  NAME\tNUMBER\tAD\tSEQ\t...\t...\t...\t
9050
I have a shell script with this code:  var=`hg st -R "$path"` if [ -n "$var" ]; then     echo $var fi   But the conditional code always executes, because hg st always prints at least one newline character.   Is there a simple way to strip whitespace from $var (like trim() in )?   or   Is there a standard way of dealing with this issue?   I could use  or , but I'd like to think there is a more elegant solution to this problem.
59568
I am looking for a command line or bash script that would add space 5 times before the beginning of each line in a file.  for example:  abc   after adding spaces 5 times        abc
33401
I'm trying to use the start command in the command prompt to open files and folders, but I'm unable to open files and folders that contain space(s) in their name.  I have tried the following queries (testing on C:\Program Files):  start C:\Program Files start C:/Program\ Files start C:/"Program Files" start C:\"Program Files" start "C:\Program Files" start "C:/Program Files" start C:/Program_Files start C:/Program%20Files   But none of them work.
97967
So, I'm using a script I've made to convert videos to the webm format. A certain program calls the script, sending %f which is the full, absolute file name of the video, like this:  converter.sh %f   where %f has two possible extensions, .avi or .mpg.  # /bin/bash ffmpeg -i $1 `dirname $1``basename $1 avi`webm &amp;&amp; rm $1   It currently works perfectly when $1 contains a .avi file, because basename removes the .avi extension. When $1 ends with .mpg instead, the result is .mpgwebm.  How can I modify that script to be able to receive those two possible different formats?    Resuming: If $1 is /somedir/video.avi, the script should do:  ffmpeg -i /somedir/video.avi /somedir/video.webm   And if $1 is /somedir/video.mpg, the script should do:  ffmpeg -i /somedir/video.mpg /somedir/video.webm   I know this might be fool for some people, but I'm kind of new with the bash.
67258
I have a text file that I can change using other applications (for example openoffice).  But when I try to change and save it using gedit, I am getting error from gedit:  Could not save the file /media/sf_Ubuntu/BuildNotes.txt. Unexpected error: Error renaming temporary file: Text file busy   the permission of BuildNotes.txt is as follow:  -rwxrwx--- 1 root vboxsf  839 2012-10-26 12:08 BuildNotes.txt   and user id is:  m@m-Linux:/media/sf_Ubuntu$ id uid=1000(m) gid=1000(m) groups=4(adm),20(dialout),24(cdrom),46(plugdev),105(lpadmin),119(admin),122(sambashare),1000(m),1001(vboxsf)   What is the problem and how I can fix it?
89739
I'm running on win2003 server, PHP 526, via the cmd-line.  I have a cmdline string:  $cmd = '  "d:\Prog Files\foo.exe" -p "d:\data path\datadir"  ';     Trying to do this in php code  $out = `$cmd`;       # note use of backticks AKA shell_exec   results in a failure by foo.exe as it interprets the -p arg as "d:\data".  However, the same $cdm string copied to the windows shell cmdline executes successfully.  How do I properly handle spaces in PHP shell_exec?
91176
The following works great on the command-line:  $ ffmpeg -i input.m4a -metadata 'title=Spaces and $pecial char'\''s' output.m4a   How do I parameterize this command and use it in a script/function? I would like to add multiple metadata tags like this:  $ set-tags.sh -metadata 'tag1=a b c' -metadata 'tag2=1 2 3'   update:  I simplified my question a little too much. I actually want to call a script that calls a script with with the parameterized command in it.  This is my exact use case:  This function converts files to audio-book format (defined in .profile):  # snippet of .profile convert_to_m4b () {     FILE="$1"     BASENAME=${FILE%.*}; shift      ffmpeg -i "$FILE" -vn -ac 1 -ar 22050 -b:a 32k "$@" tmp.m4a &amp;&amp;     mv tmp.m4a "$BASENAME.m4b" }; export -f convert_to_m4b   Function convert_to_m4b is called from download-and-convert.sh:  #/bin/sh  MP3_URL=$1; shift FILENAME=$1; shift  if [ ! -f "${FILENAME}.mp3" ]; then     curl --location --output "${FILENAME}.mp3" "$MP3_URL" fi  convert_to_m4b "${FILENAME}.mp3" "$@"   Download-and-convert.sh is called from process-all.sh:  #/bin/sh download-and-convert.sh http://1.mp3 'file 1' -metadata 'title=title 1' -metadata 'album=album 1' download-and-convert.sh http://2.mp3 'file 2' -metadata 'title=title 2' -metadata 'album=album 2' ... ... download-and-convert.sh http://3.mp3 'file N' -metadata 'title=title N' -metadata 'album=album N'   I get this error from ffmpeg:  [NULL @ 00000000028fafa0] Unable to find a suitable output format for ''@'' '@': Invalid argument   "$@" works if I inline convert_to_m4b in download-and-convert.sh instead of calling the function.    The following does not work because the quotes are lost, causing arguments with spaces to be incorrectly split up:  #/bin/sh ffmpeg -i input.m4a $@ output.m4a   I have tried various methods of , but this ends up quoting '-metadata' as well, so the command line argument is not properly recognized.  I guess I would like to only surround each argument with quotes if that argument was quoted to begin with. This seems difficult to do because bash strips the quotes before being passing arguments to the script/function.  Or is there a better method of relaying the -metadata arguments? (like environment variables or files)
75539
I have a text file with tab-separated columns which I'd like to process using awk. Here's an example of such a file: size=1\tname=foo\tweight=1.2 weight=2.5\tname=bar\tsize=2  What I want to achieve is to normalize the numeric value in columns whose content is like $field_name=&lt;number&gt; to four decimal places and keep the rest as is. Here, $field_name is a shell variable that is passed to awk and I'd like to use its value inside a regex. Here's a snippet (which is not working of course). I'm particularly interested in fixing line #5 in the following awk script and not solutions using other tools, such as sed, perl, python, etc. $ cat &quot;${file}&quot; \                                       # 1     | awk -F &quot;\t&quot; -v field_name=&quot;${external_var}&quot; '     # 2       {                                                 # 3         for (i = 1; i &lt;= NF; i++) {                     # 4           if ($i ~ /$field_name=[0-9]*.?[0-9]+/) {      # 5             split($i, kv, &quot;=&quot;)                          # 6             $i = sprintf(&quot;%s=%.4f&quot;, kv[1], kv[2])       # 7           }                                             # 8         }                                               # 9         print $0                                        # 10       }'