Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

bashrc fsck yosemite

Hey all I have a script in applescript that modifies the bashrc file in private/etc.


This works on every system I've tested except 10.10 yosemite.


What my script does is run various cleans on my systems, and in the end reboots the unit into single user mode and runs FSCK then reboots.


In Yosemite this does not happen. It reboots the unit in single user mode but then does not thing, sits at single user mode screen.


Please help. What Changed?


Part of my script.


set myname to do shell script ("whoami")



do shell script " sudo -v" with administrator privileges

try

do shell script "mkdir /Users/Shared/bash"

end try

try

do shell script "mkdir /Users/Shared/bash/original"

end try

try

do shell script "mkdir /Users/Shared/bash/modified"

end try

try

do shell script "cp Private/etc/bashrc /Users/Shared/bash/original/"

end try

try

do shell script "cp /Users/Shared/bash/original/bashrc /Users/Shared/bash/modified/"

end try


delay 1


do shell script "chmod 777 /Users/Shared/bash/modified/bashrc" with administrator privileges


delay 3


tell application "TextEdit"

open file "Users:Shared:bash:modified:bashrc"

end tell


delay 1

tell application "TextEdit"


activate


delay 5

repeat 40 times

tell application "System Events" to key code 125

end repeat



delay 1

tell application "System Events" to keystroke "singleuser=`sysctl -n kern.singleuser`"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "if [[ $singleuser -eq 1 ]]; then"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "nvram boot-args=\"\""

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "echo Starting Process -- Repair disk"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "sleep 3"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "/sbin/fsck -fy"

tell application "System Events" to keystroke return




tell application "System Events" to keystroke "echo Starting Process -- Mounting the Drive"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "sleep 3"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "sbin/mount -uw /"

tell application "System Events" to keystroke return


tell application "System Events" to keystroke "echo Starting Process -- Begin moving proper files back into place"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "sleep 3"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "mv /Private/etc/bashrc /Users/Shared/bash/"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "mv /Users/Shared/bash/original/bashrc /Private/etc/"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "rm -rfv /Users/Shared/bash"

tell application "System Events" to keystroke return


tell application "System Events" to keystroke "echo Starting Process -- Boot Computer, Give me a min"

tell application "System Events" to keystroke return


tell application "System Events" to keystroke "exit"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "else"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "echo Not in single user mode"

tell application "System Events" to keystroke return

tell application "System Events" to keystroke "fi"


tell application "System Events" to keystroke "s" using command down



end tell



delay 10

do shell script "sudo rm -rfv Private/etc/bashrc" with administrator privileges


do shell script "sudo mv /Users/Shared/bash/modified/bashrc Private/etc/bashrc" with administrator privileges



do shell script "sudo nvram boot-args=\"-s\"" with administrator privileges

try

do shell script "killall Terminal"

end try

try

do shell script "killall TextEdit"

end try

try

tell applicat

end try

try

tell application "TextEdit" to quit

end try

try

tell application "Terminal" to quit

end try



delay 2




-- restarts machine disabling reopen windows on login. Had to use restart window because without it it always reopen windows.


do shell script "defaults write com.apple.loginwindow TALLogoutSavesState -bool False"


do shell script "osascript -e 'tell app \"loginwindow\" to «event aevtrrst»'"



delay 1


tell application "System Events" to keystroke return



Posted on Dec 5, 2014 7:35 PM

Reply
4 replies

Dec 5, 2014 11:17 PM in response to John Antolino

There are so many things that need work in your script, it's hard to know where to start....


I'm guessing I know the specific problem, but the first thing to change is:



do shell script " sudo -v" with administrator privileges


In addition to being pointless (it does nothing), it's just wrong - you should not mix 'sudo' and '... with administrator privileges'. In fact, you should never use sudo in a do shell script command at all. 'with administrator privileges' takes care of elevating your privileges, and it needs to be added to each do shell script command, since they are executed as separate processes (i.e. just because one command is elevated, it does not mean the others are).


The second (probably major) problem is that you're using TextEdit to edit your bashrc, coupled with a slew of UI commands to simulate typing in the text you want. That's cumbersome. It would be far, far, far better to include the entire script as a single text object within the script and just recreate the file, or append to the existing file. You could either use another shell command to append the text, or use native AppleScript commands to write to the file, like:


property myShellCommands : "singleuser=`sysctl -n kern.singleuser`

if [[ $singleuser -eq 1 ]]; then

nvram boot-args=\"\"

echo Starting Process -- Repair disk

sleep 3

/sbin/fsck -fy

yada yada yada"


set mybashrc to (open for access file "Users:shared:bash:modified:bashrc" with write permission)

set curEOF to (get eofmybashrc)

write (ASCII character 10) & myShellCommandstomybashrcstarting atcurEOF + 1

close accessmybashrc


In this way your 60-odd lines of cruft are reduced to 4 lines and a text object.


The reason why I think TextEdit is the problem is that it doesn't always save plain ASCII files - it may adjust the line endings (CR vs CR/LF vs LF), or may save a RTF version of the file, none of which will work in the shell. Writing to the file directly avoids this problem.

Dec 7, 2014 1:52 PM in response to John Antolino

It is actually long term unwise to modify the /etc/bashrc file, as any Mac OS X update might change the file on you without preserving your changes, or if you do a "Nuke & Pave" (clean install) of Mac OS X, then use Migration Assistant, it is unlikely your customized /etc/bashrc will be transferred over.


Just my 2 cents after years and years of playing with different Unix implementations.

Oct 31, 2015 7:45 AM in response to John Antolino

I ran into a situation that required me to run fsck remotely on a system running Mac OS 10.10, and unfortunately the neither the bashrc or profile are called in single-user as of Mac OS 10.10. In my case, system was not physically accessable, so I had not other choice but to get the code working. It seems that as of Mac OS 10.10 single user mode calls /bin/sh with options that disable the checking of bashrc/profile. I was able to get around the problem by downloading the bash source, and modifiying it such that /etc/profile is called no matter what arguments are specified when calling the shell. I then replaced /bin/sh on my system with the custom one I compiled. To modify the bash source you will need to work with the lines containing: maybe_execute_file (SYS_PROFILE, 1); in shell.c Once the custom /bin/sh is installed, then the script below works perfectly to run fsck remotely.

I DO NOT recommend making these modifications to your own system.


#/etc/profile fsck script works with modified /bin/sh

singleuser=`sysctl -n kern.singleuser`

if [[ $singleuser -eq 1 ]]; then

nvram boot-args=""

/sbin/fsck -fy

reboot

else

echo Not in single user mode

fi

#Once script is installed in /etc/profile run:

#nvram boot-args="-s"

#reboot


EDIT: Formatting

bashrc fsck yosemite

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.