how do i cache password for git on mac - macos

I am following the instructions at https://help.github.com/articles/set-up-git. But when I get to the step
sudo mv git-credential-osxkeychain \
"$(dirname $(which git))/git-credential-osxkeychain"
# Move the helper to the path where git is installed
# Password: [enter your password]
I keep getting errors for dirname. dirname is my /User/myname directory. so I try replacing it with /User/myname, then with /, then with .. Each time it throws a complaint such as
-bash: dirname: is a directory
mv: git-credential-osxkeychain: No such file or directory
Or in the case of .
-bash: ????: command not found
mv: git-credential-osxkeychain: No such file or directory
But when I go to my HD I do see the file git-credential-osxkeychain there under path (ie info) /

You need to input the command
sudo mv git-credential-osxkeychain \
"$(dirname $(which git))/git-credential-osxkeychain"
exactly as it is written; don’t replace dirname with a directory name. (The point of this command is to get your shell to do that replacement itself.)
The password you’re prompted for is your computer’s password—the use of sudo is what’s triggering the password prompt here.

Related

How to delete contents of user home dir safely via bash

I am writing a bash script to do a account restore. The contents of the home dir is zipped up using this command.
sudo sh -c "cd /home/$username; zip -0 -FS -r -b /tmp /home/0-backup/users/$username.zip ."
This works as expected.
If the user requests a restore of their data, I am doing the following
sudo sh -c "cd /home/$username; rm -rf *"
Then
sudo -u $username unzip /home/0-backup/users/$username.zip -d /home/$username/
This works as expected.
However you can see the flaw in the delete statement, if the username is not set. We delete all users home dir. I have if statements that do the checking to make sure the username is there. I am looking for some advice on a better way to handle resetting the users account data that isn't so dangerous.
One thought I had was to delete the user account and then recreate it. Then do the restore. I think that this would be less risky. I am open to any suggestions.
Check the parameters first.
Then use && after cd so that it won't execute rm if the cd fails.
if [ -n "$username" ] && [ -d "/home/$username" ]
then
sudo sh -c "cd '/home/$username' && rm -rf * .[^.]*"
fi
I added .[^.]* in the rm command so it will delete dot-files as well. [^.] is needed to prevent it from deleting . (the user's directory) and .. (the /home directory).

Bash: Using asterisk in any bash command in home directory exits with error "Unrecognized options --list-sessions"

Title.
This same/similar error seems to occur with any command in bash if I:
Am in my home directory, i.e. after cd ~. It doesnt occur in other places.
Have an asterisk/wildcard in the command matching all of the files+dirs. It doesnt happen without the asterisk as far as I can tell.
Examples:
me#mypc:~$ ls *
ls: unrecognized option '--list-sessions'
Try 'ls --help' for more information.
me#mypc:~$ ls
...
Desktop
Downloads
...
(prints all directories)
me#mypc:~$ ls ./*
(prints all subdirectory subdirectories)
me#mypc:~$ ls .*
...
.vscode:
argv.json extensions
.wine:
dosdevices drive_c system.reg userdef.reg user.reg
...
(prints all subdirectories with their contents)
me#mypc:~$ du -sch *
du: unrecognized option '--list-sessions'
Try 'du --help' for more information.
me#mypc:~$ du -sch .*
du: cannot read directory './.dbus': Permission denied
du: cannot read directory './.pgadmin': Permission denied
66G .
du: cannot read directory '../postgres/.gnupg': Permission denied
64K ..
66G total
me#mypc:~$ grep "\-\-list-sessions" *
grep: unrecognized option '--list-sessions'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
And so on.
The list-sessions flag seems reminiscent of this tmux command:
me#mypc:~$ tmux list-sessions
error connecting to /tmp/tmux-1000/default (No such file or directory)
me#mypc:~$ whereis tmux
tmux: /usr/bin/tmux /usr/share/man/man1/tmux.1.gz
The list-sessions error in other commands persists with and without running tmux server.
Thanks for your help!
I'm guessing you have a file in your home directory named --list-sessions and the *, when expanded, is causing your command (eg, ls, du) to treat the file name as an option:
Consider ...
$ mkdir /tmp/ttt
$ cd /tmp/ttt
$ touch -- '--list-sessions'
$ ls -a
--list-sessions ./ ../
$ ls *
dir: unknown option -- list-sessions
Try 'dir --help' for more information.
$ ls -- * # disable further command line option processing
--list-sessions
$ du -k *
du: unknown option -- list-sessions
Try 'du --help' for more information.
$ du -k -- * # disable further command line option processing
0 --list-sessions
NOTE: The -- used in the above commands disables further command line option processing, eg, What does double dash mean?

mkdir through bash creates an unexpected temp name

When I create a directory on a mounted samba share in ssh with this command:
mkdir -p /media/networkshare/public/backups/svr2/2016_03_15_15
Everything works as expected, but when I do it through bash:
#!/bin/bash
mkdir -p /media/networkshare/public/backups/svr2/2016_03_15_15
The directory is created with a temp name like: 2AD9UB~Q
Please help
This was caused by windows line endings. I fixed it by running the file through dos2unix

How to create symlinks in a specific directory

I'm working on an automated installation of a openSUSE system using AutoYAST, and I'm stumped on a small detail. In order to setup relevant applications in the user's environment, I try to symlink to all applications located in /usr/local/bin in ~/bin (so say /usr/local/bin has the addr2line utility, then I want to have a symlink to that in ~/bin).
I've tried to execute the following snipped to accomplish this:
su -c "for program in `ls /usr/local/bin`; do ln -s /usr/local/bin/$program ~/bin/$program; done" <user>
This snippet executes in the post-script phase of the automatic installation, which is executed as root (and seeing as I want the owner of the symlinks to be the user, this command is executed using su).
However, this does not work, and gives the following output:
++ ls /usr/local/bin
+ su -c 'for program in addr2line
ar
as
c++
c++filt
cpp
elfedit
g++
gcc
gcc-ar
gcc-nm
gcc-ranlib
gcov
gprof
i686-pc-linux-gnu-c++
i686-pc-linux-gnu-g++
i686-pc-linux-gnu-gcc
i686-pc-linux-gnu-gcc-4.9.3
i686-pc-linux-gnu-gcc-ar
i686-pc-linux-gnu-gcc-nm
i686-pc-linux-gnu-gcc-ranlib
ld
ld.bfd
nm
objcopy
objdump
ranlib
readelf
size
strings
strip; do ln -s /usr/local/bin/ ~/bin/; done' <user>
bash: -c: line 1: syntax error near unexpected token `ar'
bash: -c: line 1: `ar'
I've tried several variations of the command, but all seem to not exactly do what I want.
For example, I've also tried:
su -c "for program in /usr/local/bin/*; do ln -s $program ~/bin/; done" <user>
But this only created a symlink to /usr/local/bin in ~/bin.
So I'm a bit stuck on this one... Does anybody have an idea?
You're using double quotes to define your su command, so $program is being evaluated immediately. You want it evaluated when su executes the command. Use single quotes instead:
su -c 'for program in `ls /usr/local/bin`; do ln -s /usr/local/bin/$program ~/bin/$program; done' <user>
You can also use cp -s to create symlinks on a system with GNU cp (like your suse system), which gives you the ability to use recursion and the other fun options of cp.
In the end, I decided to go with the command posted by pacholik to fix this, as my original attempt was over-engineered and thus not necessary.
ln -s /usr/local/bin/* ~/bin

Can't get shell script to run in Xcode

I'm fairly new to running scripts in Xcode and haven't been able to figure out whats wrong with the script I'm running. The first script I ran was this:
/bin/sh -x
PBXCP=${DEVELOPER_DIR}/Library/PrivateFrameworks/DevToolsCore.framework/Resources/pbxcp
${PBXCP} -exclude .svn "${PROJECT_DIR}/../../base"
"${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/"
Which caused me to run into this error:
/Users/newperson/Library/Developer/Xcode/DerivedData/appname-etesgjzdmfzimlgvakidckjecgij
/Build
/Intermediates/appname.build/Debug-iphonesimulator/app.build/Script-
435F41A90F532CA300887552.sh: line 3: /Applications/Xcode.app/Contents/Developer/Library
/PrivateFrameworks/DevToolsCore.framework/Resources/pbxcp: No such file or directory
This error was fairly to the point, The file the script is looking for doesn't exist. The newer versions of Xcode have gotten rid of pbxcp. So I started looking for a good alternative script to run that wouldn't use pbxcp, when I found this:
/bin/sh -x
/usr/bin/tar -c -C "${PROJECT_DIR}/myframeworks" --exclude .DS_Store --exclude CVS --exclude
.svn --exclude .git -H `cd "${PROJECT_DIR}/myframeworks" && find DevToolsCore.framework` |
/usr/bin/tar -x -C ${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}
This script also caused me to run into a problem, which was this:
tar: could not chdir to '/Users/newperson/Library/Developer/Xcode/DerivedData/appname-
etesgjzdmfzimlgvakidckjecgij/Build/Products/Debug-iphonesimulator/appname.app/Frameworks'
tar: Write error
Command /bin/sh failed with exit code 1
I couldn't find a clear answer to what this error meant, one forum suggest that I use the sudo command in my script to give the script permission to change directory, so I ran this:
/bin/sh -x
/usr/bin/tar -c -C "${PROJECT_DIR}/myframeworks" --exclude .DS_Store --exclude CVS --exclude
.svn --exclude .git -H `cd "${PROJECT_DIR}/myframeworks" && find DevToolsCore.framework`
| sudo /usr/bin/tar -x -C ${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}
This script caused me to run into this error though:
tar: Write error
Command /bin/sh failed with exit code 1
++ find DevToolsCore.framework
sudo: no tty present and no askpass program specified
tar: Write error
This is as far as I got so far, I am fairly lost with my limited knowledge of shell script so any help correcting my script or finding a suitable replacement for the Xcode framework that contains pbxcp would be appreciated.
Change the permissions of the directory where your script wants to write files. Do it in an interactive session of Terminal:
$ sudo chmod a+w the_directory
Then you should be able to run your script (without sudoing the tar).

Resources