0

I have shell scripts that could or could not contain Windows line breaks. I want to convert all existing Windows line breaks to Unix-style, and not destroy the file in the process. I used dos2unix on my development server, but I found out that it’s not installed on the production server, and I cannot install it there. I have to find an alternative that does the exact same thing as dos2unix. In particular, I need the result to have the same attributes as the original file: if the file was executable before conversion, it must be executable afterwards.

Stephen Kitt
  • 434,908
Jules
  • 1
  • I would expect that col command, with those specific redirections, to erase most of the data in the file pointed to by $path. Additionally, if the current shell is zsh, the path variable is special (an array containing the paths listed in $PATH), so if you changed its value, col may not be found at all (that goes for the dos2unix command as well, obviously) – Kusalananda Feb 10 '23 at 12:04
  • So is there no alternative to "dos2unix" that does the exact same thing. Nothing more, nothing less? Every other Command i read about( here too ) returns not the same result. – Jules Feb 10 '23 at 12:06
  • @Jules of course there's alternatives. But it's impossible to know what you have at hand. All that's being said here is that, to little surprise, col is not the tool. – Marcus Müller Feb 10 '23 at 12:08
  • There's also busybox dos2unix if your system has busybox. – Kusalananda Feb 10 '23 at 12:10
  • 2
    @Jules, "I found out that its not installed on the production server. And I cannot install it." - the usual approach here is that you go to your Change Board and give reasons for it to get installed. Don't work around a situation when the proper solution is to fix it. (And that might be fixing the scripts in development, not working around broken line-endings on production.) – Chris Davies Feb 10 '23 at 12:14
  • 2
    Sounds a lot like you would be much better off fixing your scripts on a development machine. – Marcus Müller Feb 10 '23 at 12:14
  • Also, if you deploy to a "production server", that means your self-written scripts are already in a source control system; so... that is probably already possible. – Marcus Müller Feb 10 '23 at 12:16
  • Jules it's helpful in the sense that you should address the underlying issue with your development environment. Then you won't need dos2unix or any other conversion solution on your production system because it's already fixed before it gets to production. – Chris Davies Feb 10 '23 at 12:16
  • The "solution" seems like commands that do more than dos2unix. And obviously i'm developing on a dev machine. I dont understand comments like this. Not helpful – Jules Feb 10 '23 at 12:17
  • 1
    @Jules The comments are saying that you should be deploying corrected scripts to the production system, instead of modifying the scripts with dos2unix on the production system before running them, which would introduce unnecessary complexity. – Kusalananda Feb 10 '23 at 12:20
  • Use the script suggested in this answer, @Jules. The command that does the work is tr. – Chris Davies Feb 10 '23 at 12:21
  • @roaima thanks i'll look into it – Jules Feb 10 '23 at 12:23
  • 1
    @Jules you got very detailed expert answers. Please try to understand that the solution to the problem "I have a production server on which I want to fix scripts" is, no matter whether you have dos2unix or any other tool on that server, "so go and fix your scripts on your developer laptop/PC/whatever, and deploy." It's not a production server if the approach to scripts is that you fix them on the server. – Marcus Müller Feb 10 '23 at 12:41
  • All answers in the other post don't do the same. dos2unix only changes the file if there are windows line breaks. So my question was not answered. FYI.

    Thanks to the people who tried to help without being condescending.

    – Jules Feb 10 '23 at 15:25
  • Heres the function i use in my script: `copyAllFiles() { replaceStr="/var/" searchStr="/home/" echo "copying files from: $1" for f in "$1"/* do if [ -d $f ] then copyAllFiles $f else
    filename=$(basename -- "$f") extension="${filename##*.}"

    newPath=$(echo "$f" | sed "s#/home/jwe/#/var/#g") echo "Copy $f to $newPath" cp "$f" "$newPath"

    if [ "$extension" = "sh" ] then echo "Convert shell script to Unix $newPath" dos2unix $newPath fi fi done }`

    – Jules Feb 10 '23 at 15:55
  • The sed command given here will preserve the executable bit. – Stephen Kitt Feb 10 '23 at 15:58
  • @StephenKitt thanks i'll try that command – Jules Feb 10 '23 at 15:59
  • @StephenKitt thanks a lot. That seems to work! – Jules Feb 10 '23 at 16:08
  • @Jules Please keep this site's Code of Conduct in mind when interacting with others on this site. Some of your recent comments have cross the line and have been deleted. Thank you. – Jeff Schaller Feb 10 '23 at 17:07

0 Answers0