How to source an external file in .bash_profile macOS - bash

I tried to move my script from linux to mac, folder ~/Scripts placed in home with all other scripts,
but using source in .bash_profile won't call my script.
Contents of .bash_profile
f [ -f ~/.bashrc ]; then
. ~/.bashrc
. ~/Scripts/ipmitool
fi
alias test='echo test from bash_profile'
Contents of /Scripts/ipmitool
#!/bin/bash
if [[ -z $1 ]] ; then
printf "ipmitool <Host IP> \n\n"
else
ssh con_1 ipmitool -I lanplus -U * -P * -H $1 fru print
fi
Terminal
1232324fa:~ OiO$ ipmitool
-bash: ipmitool: command not found
1232324fa:~ OiO$ bash ~/Scripts/ipmitool
ipmitool <Host IP>
I need to use bash in front to be able to call ipmitool, in linux I just need to type ipmitool.
I tried to follow this How to source an external file in .bash_profile in OSX? but didn't work.
Could someone help this newb, Thanks.

It doesn't look like you want to source it. You want to add ~/Scripts to your path, so that later you can type ipmitool rather than ~/Scripts/ipmitool to execute it.
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
PATH=~/Scripts:$PATH
alias test='echo test from bash_profile'
(Make sure ~/Scripts/ipmitool is executable, as well, by running
chmod +x ~/Scripts/ipmitool
)

Related

How to use mkdir -v as defaul mkdir in Linux

In linux ,when I use mkdir -v dir1 ; linux will show me some information like "dir1 create
" "dir1 exits" ;
mkdir -v make me fell a "Sense of Security" , i like it .
But I want make "mkdir -v " as the default "mkdir" , everytime I use mkdir , it will show me infomation ;
I think this idea beacause everytime i use "mkdir -v", i feel it unconvinient .
I have try some method , like export " alias mkdir="mkdir -v " " in my .bashrc , but it didn't work.
Any easy method to achieve this idea ? Thank you very much !
You will have to define the alias to do this
The command below will append to your .bashrc and will accomplish the desired result
echo 'alias mkdir="mkdir -v"' >> ~/.bashrc
Remember to get a new shell or type bash to reload your .bashrc file in your current shell
If once in a while you don't want this behavior just prefix your command with a \
\mkdir test
The above will ignore the effect of alias just for this one command.
Sample run
$ alias mkdir='mkdir -v'
$ mkdir test
mkdir: created directory 'test'
$ \mkdir test1
$

How do I write a script that shows all aliases upon startup of terminal

I am a new user learning to use Linux. I am currently running Ubuntu 18.04 with several aliases created, and saved in the ~/.bashrc directory. I am trying to write a welcome script that also displays the current aliases upon start up. The current code I have is as follows:
#! /bin/bash
echo -e "\nWelcome $USER"
echo -e "Today's date is: \c"
date
echo -e "\vHave \vA \VGreat \vDay! \c"
echo -e "\nCurrent aliases for reference are:"
alias
Upon startup, or running the script on it's own, the welcome message runs but the actual alias command does not?
First things first:
(...) saved in the ~/.bashrc directory. (...)
Well, I must point that .bashrc is a file, not a directory, and is part of the Bash startup files.
That said, the reason why running the alias command inside a script does not work as expected is that it is a shell builtin, and when invoking it from a script will not behave as if running it from your shell.
Hence, the quickest thing you can do is store your aliases in a different file, like ~/.bash_aliases and ensure it will be loaded by adding this to your .bashrc file:
if [ -f ~/.bash_aliases ]; then
source ~/.bash_aliases
fi
And then read that file directly from your script:
#! /bin/bash
echo -e "\nWelcome $USER"
echo -e "Today's date is: \c"
date
echo -e "\vHave \vA \VGreat \vDay! \c"
echo -e "\nCurrent aliases for reference are:"
cat ~/.bash_aliases

terminal title not setting within screen

Currently, I'm setting terminal title within screen command, but the bash script gives me:
Cannot exec 'source /etc/profile && title.set root#test': No such file or directory
And I can run above command successful directly from the command line, here are my scripts:
/usr/local/bin/s
#!/bin/bash
if [ $1 ]
then
screen -D -R $1 -m "source /etc/profile && title.set `whoami`#$1"
else
screen -R
fi
/etc/profile
...
# Source global bash config
if test "$PS1" && test "$BASH" && test -z ${POSIXLY_CORRECT+x} && test -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi
function title.set() {
if [[ -z "$ORIG" ]]; then
ORIG=$PS1
fi
TITLE="\[\e]2;$*\a\]"
PS1=${ORIG}${TITLE}
}
# Termcap is outdated, old, and crusty, kill it.
unset TERMCAP
# Man is much better than us at figuring this out
unset MANPATH
...
So What's going wrong here?
The keyword source is a bash built-in command, i.e., something for which there is not necessarily an actual file to exec (another built-in command). You can only exec something that is a file — like bash, e.g., something like this:
screen -D -R $1 -m bash -c "source /etc/profile && title.set `whoami`#$1"

Read response and if then else loop

I have this piece of rudementary code in my .bash_profile that loads on login, but I can't get it working. Probably some easy fix, but I', staring my self blind on it right now.
The code:
# Simple backup when editing files with nano
function bu() {
read -p "Backup >>"`basename $1`"<< b4 edit [Y/n]?" response
echo $response
response=$response${response,,} # tolower
if [[ $response =~ ^(yes|y| ) ]]; then
mkdir -p ~/.backup
#cp -v "$1" ~/.backup/`basename $1`-`date +%Y%m%d%H%M`.backup
cp "$1" ~/.backup/`basename $1`-`date +%Y%m%d%H%M`.backup
echo ~/.backup/`basename $1`-`date +%Y%m%d%H%M`.backup >> ~/.backup/bu_log.txt
nano "$1"
else
nano "$1"
fi
}
And it has an alias nano="bu"
so, when i write nano, it should ask me if i want to backup the file first (on yes) or just open it in nano straight away.
The only thing that happens now is that it keeps asking the question and looping, newer goes to nano.
CentOS is the linux
Since nano is an alias to bu, typing nano runs your function, which calls nano, which is an alias to bu, which calls nano, ...
In order to run the actual nano editor, you need to disable alias expansion for that call. Use the command built-in:
command nano "$1"
You are calling nano recursivly, since you aliased nano=bu
so try to change the line in the script
nano "$1"
to the full path of
/usr/bin/nano "$1"
(or where nano is installed on your system)
I think you want:
response=${response,,}
You have
response=$response${response,,}
which gets you response=Yy. That won't match your regular expression.
You could also just do shopt -s nocasematch.
Aliases are usually trouble. The rule is "if in doubt, use a function."
nano() {
bu "$#"
}
bu() ( # Use a subshell to avoid having to reset shell options
shopt -s nocasematch
local base=${1##*/}
read -p "Backup >>${base}<< b4 edit [Y/n]?" response
case $response in
y*)
mkdir -p ~/.backup
local backup=~/.backup/"${base}-$(date +%Y%m%d%H%M`).backup"
cp "$1" "$backup"
echo "$backup" >> ~/.backup/bu_log.txt
;;
esac
command nano "$#" # Use "$#" to allow you to pass more than one argument to nano
)

Cannot Create Directories On Ubuntu With Bash Shell Script

I'm trying to run this bash shell script to create directories for vim syntax highlighting on Ubuntu 13.04 (via Vagrant 1.4.1 on Windows 7).
#!/usr/bin/env bash
basevim="$HOME/.vim"
ftdetect="${basevim}/ftdetect"
indent="${basevim}/indent"
syntax="${basevim}/syntax"
echo "Setting up VIM for syntax highlighting"
#Create directories for vim syntax highlighting
if [ ! -d "$basevim" ]; then
echo "Adding VIM syntax highlighting dirs"
mkdir "$basevim"
mkdir "$ftdetect"
mkdir "$indent"
mkdir "$syntax"
else
if [ ! -d "$ftdetect" ]; then
mkdir "$ftdetect"
fi
if [ ! -d "$indent" ]; then
mkdir "$indent"
fi
if [ ! -d "$syntax" ]; then
mkdir "$syntax"
fi
fi
This is executing as a provision.sh script for Vagrant so as far as I know it should run as root. I can see the echo'd message so it's taking the first branch. But for the life of me I can't seem to get this to work; no complaints but the directories don't get created. If I set those variables on an interactive prompt, I need to do sudo mkdir ftdetect (etc.) to get the directories created. Strangely I don't need to sudo to get the .vim directory created--at least that's what I recall.
I tried
if [ ! -d "${basevim}" ]; then
but that didn't do anything. I also tried
basevim="{$HOME}/.vim"
--also no dice. Any thoughts of what I may be missing? As I say, as far as I know it shouldn't be necessary to sudo on a provisioning script on Vagrant. I can tell the script is getting run because those echo'd messages are getting output.
Your script could be replaced by
mkdir -p "$HOME/.vim"/{ftdetect,indent,syntax}
As for the directories not appearing... Where are you looking for them?
Running this as root would create them in root's home directory, /root/, and not in the user's home directory /home/username. When in doubt, use absolute path names (and chown as needed afterwards).

Resources