3

I have a script that alters lines in files. Which was working fine, but from hence forth, files will be read-only. I've tried changing wq to wq! (as I would in VI) but this has seemingly no effect. I've also tried "zz" which also did nothing.

I appreciate any help; thank you.

debug=false

## *****Put file name in quotes******
declare -a arr=("UF19905217" "UG19905218" )

##Put date in DDMMYYYY format for the date the message was original processed.
DATE="25082015"

## now loop through the above array
for i in "${arr[@]}"
do
    #if "$debug; then
        echo "Fix file named: Inbound_$i.msg"
        MSG="Inbound_$i.msg"
    #fi

    if [ ! -d "$MSG" ]; then
    # Enter what you would like changed here.  You can copy and paste this command for multiple changes

        #DATATYPE
        printf "%s\n" ',s/<DataType>EDI<\/DataType>/<DataType>830<\/DataType>/g' wq | ed -s  /data1/Inbound/$DATE/$MSG        

        echo "Complete"
    else
            echo "Message not found or errored!"
    fi

done
tripleee
  • 175,061
  • 34
  • 275
  • 318
FrankCapone
  • 51
  • 2
  • 7
  • Use `chmod` to change the file permissions before (and after) you need to edit them? – Etan Reisner Mar 24 '16 at 13:22
  • 1
    Any particular reason you are not using `sed`? – tripleee Mar 24 '16 at 13:31
  • You'll thank yourself repeatedly for switching to trivially machine-readable sortable date format YYYYMMDD instead. – tripleee Mar 24 '16 at 13:32
  • @tripleee the only reason I'm not using sed is because I do not know how... Is that looking like my best option? – FrankCapone Mar 24 '16 at 13:36
  • @FrankCapone using another editor won't change anything, it's the underlying OS and/or filesystem that refuses you write access if I'm not mistaken. – Aaron Mar 24 '16 at 13:39
  • In vi(m), :wq! works by deleting the file, recreating it, and writing to it. That requires a bit more logic than may be comfortable in a single ed invocation. I would recommend doing it within the shell script. – Kevin M Granger Mar 24 '16 at 13:50
  • @KevinMGranger wouldn't it be `:w!q` though? I think `:wq!` would fail to write the file, then exit regardless of the unsaved modifications. – Aaron Mar 24 '16 at 13:52

2 Answers2

0

If you have write access on the directory containing the file, you can delete them before rewriting them. If you have access to GNU sed, that is what sed -i will do.

I'm not familiar with ed, but I think the following sed command would do the equivalent of your command :

sed -i 's/<DataType>EDI<\/DataType>/<DataType>830<\/DataType>/g' /data1/Inbound/$DATE/$MSG 

Also, if you are not the owner of the file, the directory must not have the sticky bit set or you won't be able to delete the file.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Hi, Aaron. I could, but the issue is that all the files will now be read-only. What I need is a solution that is able to force the write even though they are read only. I appreciate the suggestion though. – FrankCapone Mar 24 '16 at 13:30
  • You should fix your title then (which mention force quit, rather than force write), and maybe be a little more descriptive in your question. I think you may be able to come around the read-only constraint by reading, deleting and rewriting the files, I'll update my answer if I validate this solution. – Aaron Mar 24 '16 at 13:38
  • Yup it works, and it's done in one step with `sed -i`. – Aaron Mar 24 '16 at 13:48
0

Add this to your .vimrc:

" Allow saving of files as sudo when I forgot to start vim using   sudo.
cmap w!! w !sudo tee > /dev/null %

And then use ":w!!" and then ":q" instead of ":wq"

For a explanation of the command see this question

Community
  • 1
  • 1
GiftZwergrapper
  • 2,602
  • 2
  • 20
  • 40