When compiling, I always place the build in a separate directory. For example:
mkdir build
cd ./build
(cd ..; ./bootstrap)
../configure
make
Since I have plenty of RAM the aim is to compile on a TMPFS.
The script gets the name of the project, uses it for the name for the directory created in $XDG_RUNTIME_DIR/build and finally links it.
# setup-build.sh
#!/usr/bin/bash
set -e
my_project_name=$(basename $(pwd))
my_project_build_dir="$XDG_RUNTIME_DIR/build/$my_project_name"
mkdir -p $my_project_build_dir
ln -s "$my_project_build_dir" "$(pwd)/build"
The script runs without a problem. But, when I do cd ./build; ../configure it returns an error: bash: ../configure: No such file or directory. The file most certainly does exist, but Bash can't find it!
I altered the script to this:
#!/usr/bin/bash
set -e
my_project_src_dir="$(pwd)"
my_project_name="$(basename $(pwd))"
my_project_build_dir="$XDG_RUNTIME_DIR/build/$my_project_name"
mkdir -p "$my_project_build_dir"
ln -s "$my_project_build_dir" "$(pwd)/build"
cd "$my_project_build_dir"
echo "$my_project_src_dir" > "./project-src-dir.txt"
To compile I have to type cd ./build; $(cat ./project-src-dir.txt)/configure; make. This causes Bash complete to partial break, though. As in I can't TAB complete file names from $my_project_src_dir with this method, but TAB completion for arguments works fine. Ifautoconf is needed: (cd $(cat ./project-src-dir.txt); ./bootstrap). If anyone has any other ideas I would still prefer to be able to just do ../configure, although this will have to do for now.
Edit: Had to change my_project_name="$(basename '$my_project_src_dir') to my_project_name="$(basename $(pwd))" as it was taking '$my_project_src_dir' literally.
Related
I'm looking for a command that would create a directory and bring me to it directly after, similar to:
$ mkdir project-one-business-dev-2
$ cd project-one-business-dev-2
I don't want to type the project's name twice because it's too long (I know I can use tab, but what if there are similar names?). Maybe only one command can do it.
A process can't change the working directory of it's parent process. That makes it impossible for an external command like mkdir to set the working directory of the calling shell to the newly created folder.
But you can create a bash function for that purpose. Put this for example into your .bashrc:
mkcd() {
mkdir -p "${1}"
cd "${1}"
}
You can do it like this:
mkdir project-one-business-dev-2 && cd "$_"
for more information check out this post on AskUbuntu
All,
I have a shell script which is attempting to both copy and install a .deb as part of a late_command. I used what I found from this link as a guide.
https://gist.github.com/moonwitch/11100762
Here are the echo commands appending things to the end of lubuntu.seed
echo "d-i preseed/late_command string \ " >> "$WORK_DIR/preseed/lubuntu.seed"
echo "cp /cdrom/pool/extras/my.deb . ; \ " >> "$WORK_DIR/preseed/lubuntu.seed"
echo "d-i preseed/late_command string in-target dpkg -i /cdrom/pool/extras/my.deb ; " >> "$WORK_DIR/preseed/lubuntu.seed"
What really honks me off is the cp command doesn't appear to leave the file on the target no matter what I do. There are some other things I would like to copy and unzip.
I'm working with Ubuntu 15 32-bit. Really don't care about tools that "used to work" with earlier releases. I have spent 3 days searching through every discussion and every example/tool was for a really old version of Ubuntu. Haven't found one which still works today. Some kind of run, but they don't fully function.
My current problem is due to the fact I'm not a packager person, but this has to get done and I didn't get out of the way fast enough.
What I need is an actual functioning cp statement which copies a file from the CD to the target and leaves it there through reboot. So far nothing has.
Thank you
This is the line I used for the late command:
d-i preseed/late_command string cp /cdrom/somefile /target/system/folder && cp /cdrom/someshellscript.sh /target/system/folder && chroot /target chmod a+x /target/system/folder/someshellscript.sh && chroot /target sh /system/folder/someshellscript.sh
I appended the line above to my preseed file. This line basically copies some script from the cdrom or iso to some folder in the target system. During installation the /target folder is the "target" systems root system (get it?).
So for me I copied all the files I need from the CD or iso to some folder like root or opt or even tmp in the target system. Then I changed root to /target and executed all the regular linux commands. Wrote a shell script that executes after all the copying is done to do what I want it to do.
Note:
Inside my shell script, I unset all of the installer's environment stuff:
unset DEBCONF_REDIR
unset DEBCONF_FRONTEND
unset DEBIAN_HAS_FRONTEND
unset DEBIAN_FRONTEND
Then proceed accordingly.
Hope this helps.
I'm trying to run a script that installs some files in a directory a user specifies. Once the user specifies the directory, I'd like to transfer the main file to that directory so it can perform so more tasks there before ultimately deleting itself once complete.
#prompt for directory in which to build project
read -p "Drag and drop the directory in which you'd like to build this project: "
echo "reply is $REPLY"
cp ./myScript.sh $REPLY
/bin/bash $REPLY/myScript.sh
I've got the script to execute the file from this question. I tried doing it with source $REPLY/myScript.sh as well as simply sh $REPLY/myScript.sh. I get the error /path/to/file/ is a directory
It must be that it doesn't known I'm trying to run myScript.sh, but I don't understand how I've given it a directory.
A likely cause is that drag-and-drop is putting whitespace after the directory name.
Thus:
/bin/bash $REPLY/myScript.sh
would be running
/bin/bash /path/to/directory /myScript.sh
A simple fix, if that's only a standard space, would be:
/bin/bash "${REPLY% }/myScript.sh"
You are missing the variable in read command so obiously it will fail as whatever you are reading is not getting stored. You can replace the read command as follows.
#prompt for directory in which to build project
read -p "Drag and drop the directory in which you'd like to build this project: " REPLY
How to run a Linux shell command from a different directory without actually getting there?
In the following, I want to run a make command, but without getting into the source code directory, i.e., from my home directory:
me#mypc:~$ ~/my/source/code/directory/make #This is wrong!
I have seen some examples which suggest as:
me#mypc:~$ cd ~/my/source/code/directory; make
But this ends up taking me into that source code directory, which I want to avoid.
There could the be the other option:
me#mypc:~$ cd ~/my/source/code/directory; make; cd ~
But it becomes complicated in cese.
I am wondering if there could be some way nicer and simpler than these?
You can try:
me#mypc:~$ (cd ~/my/source/code/directory; make)
Parentheses tell your shell to spawn a separate subshell, with its own current directory, and run the commands inside that subshell. After the command finishes, the subshell is closed, and you're back to your main shell, whose current directory never changed.
Do it in a subshell, e.g.
(cd ~/my/source/code/directory; make)
Alternately, you can use make's -C option, e.g.
make -C ~/my/source/code/directory
You can also use
pushd ~/my/source/code/directory; make; popd
or
current=`pwd`; cd ~/my/source/code/directory; make; cd "$current"
$ (cd directory && make)
You need to use && instead of ;. Consider something like
cd junk && rm -rf *
With &&, bash will abort if the directory junk does not exist. If you try cd junk; rm -rf * and junk doesn’t exist, you’ll delete everything in the current directory :(
I wrote my own custom post-merge hook, now I added a "hooks" directory to my main project folder (since git doesn't track changes in .git/hooks), somewhere I read that I can make a symbolic link from hooks to .git/hooks so I don't have to copy the file from one folder to the other every time someone changes it so I tried:
ln -s -f hooks/post-merge .git/hooks/post-merge
But it doesn't seem to work, any ideas why? "ln hooks/post-merge .git/hooks/post-merge" works fine but making a hard link is the same as copyin I guess....
you just used wrong path, it should be:
ln -s -f ../../hooks/post-merge .git/hooks/post-merge
While you can use symbolic links, you can also change the hooks folder for your project in your git settings with :
git config core.hooksPath hooks/
Which is local by default so it won't ruin git hooks for your other projects. It works for all hook in this repository, so it's especially useful if you have more than one hook.
If you already have custom hooks in .git/hooks/ that you do not want to share with your team you can add them in hooks/ and add a .gitignore so they're not shared.
Changing directory before linking
cd /path/to/project-repo/.git/hooks
ln -s -f ../../hooks/post-merge ./post-merge
The path calculation is done relative to the symlink. Let's understand using an example,
ln -s path/to/file symlink/file
Here, the path to the file should actually be the relative path from the symlink path.
The system actually calculates the file path as symlink/path/path/to/file
The above command should be re-written as
ln -s ../path/to/file symlink/path
The folder structure being,
/code
------ symlink/file
------ path/to/file
Utilizing Michael Cihar's comment, here is an example of a bash script I wrote to simply create these symlinks. This script is located in git_hooks/ dir which is at the project root. My .git/ folder is also in the same directory level.
#!/usr/bin/env bash
pwd=$(pwd);
# Script is designed to be ran from git_hooks/ dir
if [[ "$pwd" == *"git_hooks"* ]]; then
files=$(ls | grep -v -e '.*\.');
while read -r file; do
ln -s ../../git_hooks/$file ../.git/hooks/
echo "Linked $file -> ../.git/hooks/$file"
done <<< "$files";
else
echo "";
echo "ERROR: ";
echo "You must be within the git_hooks/ dir to run this command";
exit 1;
fi
My script must be ran from within the actual git_hooks/ directory. You can modify it to behave differently, if you'd like.
This script will symlink any file that is not suffixed with a file extension within the git_hooks/ directory. I have a README.txt in this directory + this script (named symlink.sh). All the actual git hooks are named 'pre-commit', 'pre-push', etc. so they will be symlinked.
why not just
cp ./hooks/* .git/hooks/
this worked for me in Mac OS