animal_name=""
if [[ -z "${!animal_name// }" ]]; then
animal_name="doggo"
fi
echo sudo chmod ...... ${!animal_name}
Is there any way to reference a variable in bash?
Ditch the "{}"
animal_name=""
if [[ -z "$animal_name" ]]; then
animal_name="doggo"
fi
sudo chmod {flag} $animal_name
I think your error is in line 3 (or 2, depending on how you look at it.)
animal_name=""
if [[ -z "${!animal_name// }" ]]; then # Error: event not found: animal_name//
animal_name="doggo"
fi
echo sudo chmod ...... $animal_name
The fix that I found was to remove the 2 slashes in "${!animal_name// }" and that worked for me.
If you are just trying to make sure the variable has some value, you can use defaults.
echo sudo chmod ...... ${animal_name:-doggo}
This will use doggo is animal_name is empty, but will leave it empty.
$: animal_name=
$: echo sudo chmod ...... ${animal_name:-doggo}
sudo chmod ...... doggo
$: echo "[$animal_name]" # still empty
[]
$: animal_name=bob
$: echo sudo chmod ...... ${animal_name:-doggo} # doesn't change is already set
sudo chmod ...... bob
If you want animal_name to ALWAYS default to doggo thereafter, use = instead of - and it will assign it as well if it's empty.
echo sudo chmod ...... ${animal_name:=doggo}
so:
$: animal_name=
$: echo sudo chmod ...... ${animal_name:=doggo}
sudo chmod ...... doggo
$: echo "[$animal_name]" # this time it was set
[doggo]
$: animal_name=bob
$: echo sudo chmod ...... ${animal_name:=doggo} # doesn't change if already set
sudo chmod ...... bob
Probably you mean indirection? This may help.
animal_name=""
doggo="I am doggo!"
if [[ -z "${animal_name}" ]]; then
animal_name="doggo"
fi
echo sudo chmod ...... ${!animal_name}
The ${!animal_name} reference the value of doggo, so the output is:
sudo chmod ...... I am doggo!
Related
GNU nano 2.7.4 File: /home/pi/initDisplay/initDisplay.sh
#!/usr/bin/env bash
#HDMI connection?
rm -f hdmi.name
tvservice -n 2>hdmi.name
HDMI_NAME=`cat hdmi.name`
echo $HDMI_NAME
if [ "$HDMI_NAME" == "[E] No device present" ]; then
LCD_ON=`cat /boot/config.txt | grep "#CONFIGURAZIONEHDMI"`
echo $LCD_ON
if [ "$LCD_ON" == "#CONFIGURAZIONEHDMI" ]; then
echo "reboot con la configurazione LCD"
sudo rm -f /boot/config.txt
sudo cp /boot/config_lcd.txt /boot/config.txt
sleep 2
sudo reboot -n
fi
else
HDMI_ON=`cat /boot/config.txt | grep "#CONFIGURAZIONELCD"`
echo $HDMI_ON
if [ $HDMI_ON == "#CONFIGURAZIONELCD" ]; then
echo "reboot con la configurazione HDMI"
sudo rm -f /boot/config.txt
sudo cp /boot/config_hdmi.txt /boot/config.txt
sleep 2
sudo reboot -n
fi
fi
Doesn't start the arg of if statement with $LCD_ON. When I try to execute it, it doesn't return what I expect. Now it returns:
[E] no device detected
#CONFIGURAZIONEHDMI
but it doesn't start to replace file and reboot.
P.S.: The user and the file have privileges to do it
And I already set chmod 777 the file
There might be more on the line that matches, such as extra whitespace, so the equality test doesn't match exactly.
If you want to test whether a matching line exists in a file, you can just test the exit status of grep, rather than storing the output in a variable.
if grep -q "#CONFIGURAZIONEHDMI" /boot/config.txt; then
echo "reboot con la configurazione LCD"
sudo rm -f /boot/config.txt
sudo cp /boot/config_lcd.txt /boot/config.txt
sleep 2
sudo reboot -n
fi
The -q option tells grep not to print the matching line, it just sets its exit status.
I have tried in many ways but couldn't get it the right way.
fun="
mkcdo ()
{
mkdir -p -- \"'$1'\" && cd -P -- \"'$1'\"
}"
echo "$fun" >> ~/.bashrc
What I want is to append this in .bashrc
mkcd ()
{
mkdir -p -- "$1" && cd -P -- "$1"
}
Could that be done? Is there a way in bash like there is in python: r'whatever\you\$write' so that it is completely ignored as simple text?
Using a variable to store a bash function code sounds much of anti-pattern. For multi-line formatted strings, I would recommend using here-doc and quote them to avoid expanding the variable,
cat >> ~/.bashrc << 'EOF'
mkcd () {
mkdir -p -- "$1" && cd -P -- "$1"
}
EOF
Further reading - Bash - Here documents
Alternative1: just use single quotes.
fun='
mkcdo ()
{
mkdir -p -- "$1" && cd -P -- "$1"
}'
echo "$fun" >> ~/.bashrc
Alternative2: escape the $ sign.
fun="
mkcdo ()
{
mkdir -p -- \"\$1\" && cd -P -- \"\$1\"
}"
echo "$fun" >> ~/.bashrc
I have the following problem. I need to create system-wide JDK_HOME and JAVA_HOME variables. First I want to create /etc/profile.d/java.sh and add
JDK_HOME to it. Then I want to append JAVA_HOME to this file. So far I have this code.
#!/bin/bash
create_env_var()
{
local varname="$1"
local varvalue="$2"
local filename="/etc/profile.d/$3"
if [ -e "$filename" ]; then
echo "**ERROR: file $filename already exists"
else
sh -c 'echo "$varname=$varvalue" > $filename'
chmod +x "$filename"
fi
}
append_env_var()
{
local varname="$1"
local varvalue="$2"
local filename="/etc/profile.d/$3"
if [ ! -e "$filename" ]; then
echo "**ERROR: file $filename not found"
else
sh -c 'echo "$varname=$varvalue" >> $filename'
chmod +x "$filename"
fi
}
create_env_var "JDK_HOME" "/usr/lib/jvm/java-7-openjdk-i386" "java.sh"
append_env_var "JAVA_HOME" "/usr/lib/jvm/java-7-openjdk-i386" "java.sh"
exit "$?"
However these lines don't work and I see the following errors:
sh: 1: cannot create : Directory nonexistent
chmod: cannot access ‘/etc/profile.d/java.sh’: No such file or directory
Would you please show me where everything goes wrong?
While you can remove the single quotes, there's no reason to create a subprocess and use sh -c.
change sh -c 'echo "$varname=$varvalue" > $filename'
to echo "$varname=$varvalue" > $filename
and sh -c 'echo "$varname=$varvalue" >> $filename'
to echo "$varname=$varvalue" >> $filename
My problem is that i don't understand why when I run the script if i write "yes" or "no" it always says I'm quitting the script. I know that it doesn't recognise $reply and "yes" as equal but why? (I am new in bash programming). Thanks!
#!/bin/bash
clear
echo 'This script will install: Firefox 17.0.1 (language: enGB or itIT or enUS) and flash 11 in Firefox17.0.1, continue?'
read reply
if (( "$reply" = "yes" )); then
pkill firefox
rm -rf /tmp/fox
mkdir /tmp/fox
cd /tmp/fox
rm -rf /opt/firefox/*
rm -rf /usr/lib/mozilla/plugins/*
rm -f /usr/share/icons/mozicon128.png
mkdir /usr/lib/mozilla/plugins
mkdir /opt/firefox
echo "Enter your language (exmp: it en us)"
read reply1
if (( "$reply1" = "it" )); then
wget ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/linux-x86_64/it-IT/firefox-17.0.1.tar.bz2
elif (( "$reply1" = "en" )); then
wget ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/linux-x86_64/en-GB/firefox-17.0.1.tar.bz2
else
wget ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/linux-x86_64/en-US/firefox-17.0.1.tar.bz2
fi
wget http://fpdownload.macromedia.com/get/flashplayer/pdc/11.2.202.258/install_flash_player_11_linux.x86_64.tar.gz
wget http://upload.wikimedia.org/wikipedia/commons/7/76/Mozilla_Nightly_icon_2011.png
tar -xvf firefox-17.0.1.tar.bz2
tar -xvf install_flash_player_11_linux.x86_64.tar.gz
cp -R firefox/* /opt/firefox
cp libflashplayer.so /usr/lib/mozilla/plugins/
rm -f /opt/firefox/icons/mozicon128.png
cp -f Mozilla_Nightly_icon_2011.png /opt/firefox/icons/mozicon128.png
echo "Reboot the system to show the new icon?"
read response
if (( "$response" = "yes" )); then
reboot
else
echo 'Installation Complete'
fi
else
echo "I'm quitting the script..."
exit
fi
You're using an arithmetic expression block (( )) to compare strings. Chaos will ensue.
read -p 'This script will install: Firefox 17.0.1 (language: enGB or itIT or enUS) and flash 11 in Firefox17.0.1, continue?' reply
if [[ "$reply" = "yes" ]]; then
…
Double parenthesis (( ... )), when used with an if/then construct, return an exit status according to the evaluation of the integer test expression they are embracing. For instance, the test ((5 == 5)) would evaluate true (i.e. 0).
Standalone, double parenthesis allow for you to use arithmetic evaluations like i=$(( 2+3 )), and/or c-style syntax, like
(( i = 1 )) #or
(( i++ )) #or
for ((i=0;i<5;i++)); do ...; done
However, in your code, while you are trying to evaluate variable comparison expressions, you are in fact testing against a c-style assignment. (( "$reply" = "yes" )) does neither compare integers nor any other values, but simply assigns a numerical conversion of the string "yes" to a variable identified by the content of $reply and returns the assigned value as its exit status.
yes converts to 0. In the case that a user entered yes as their reply, the effect will be the assignment
yes=0
Given that, the if statement is merely testing for an integer value to be nonzero, so since the assigned value in the parenthesis is zero, it will return a 1 as an exit code, which means false, and the then block will not be executed. Unless the user enters a nonzero integer value.
Change parenthesis to double-brackets [[...]] and you will be fine. If you prefer to use = or == as an operator doesn't matter in this case.
if [[ "$reply" = "yes" ]]; then
You will find useful answers on this site when in need for further details.
Run this script, It worked for me with no errors and got my flash player working.
# Prepare Directory's
pkill firefox
mkdir /tmp/fox
cd /tmp/fox
rm -rf /opt/firefox/*
rm -rf /usr/lib/mozilla/plugins/*
rm -f /usr/share/icons/mozicon128.png
mkdir /usr/lib/mozilla/plugins
mkdir /opt/firefox
# Download Resources
wget http://ftp.mozilla.org/pub/mozilla.org/firefox/releases/latest/linux-x86_64/en-US/firefox-18.0.tar.bz2
wget http://fpdownload.macromedia.com/get/flashplayer/pdc/11.2.202.258/install_flash_player_11_linux.x86_64.tar.gz
wget http://upload.wikimedia.org/wikipedia/commons/7/76/Mozilla_Nightly_icon_2011.png
# Extract Resources
tar -xvf firefox-18.0.tar.bz2
tar -xvf install_flash_player_11_linux.x86_64.tar.gz
# Install
cp -R firefox/* /opt/firefox
cp libflashplayer.so /usr/lib/mozilla/plugins/
#Restart Backtrack to reload start menu icon's or goto menu editor and reselect icon
cp -f Mozilla_Nightly_icon_2011.png /usr/share/icons/mozicon128.png
# Done
echo 'Installation Complete'
readlink -f does not exist on MacOS. The only working solution for Mac OS I managed to find on the net goes like this:
if [[ $(echo $0 | awk '/^\//') == $0 ]]; then
ABSPATH=$(dirname $0)
else
ABSPATH=$PWD/$(dirname $0)
fi
Can anyone suggest anything more elegant to this seemingly trivial task?
Another (also rather ugly) option:
ABSPATH=$(cd "$(dirname "$0")"; pwd -P)
From pwd man page,
-P Display the physical current working directory (all symbolic links resolved).
Get absolute path of shell script
Dug out some old scripts from my .bashrc, and updated the syntax a bit, added a test suite.
Supports
source ./script (When called by the . dot operator)
Absolute path /path/to/script
Relative path like ./script
/path/dir1/../dir2/dir3/../script
When called from symlink
When symlink is nested eg) foo->dir1/dir2/bar bar->./../doe doe->script
When caller changes the scripts name
It has been tested and used in real projects with success, however there may be corner cases I am not aware of.
If you were able to find such a situation, please let me know.
(For one, I know that this does not run on the sh shell)
Code
pushd . > /dev/null
SCRIPT_PATH="${BASH_SOURCE[0]}";
while([ -h "${SCRIPT_PATH}" ]) do
cd "`dirname "${SCRIPT_PATH}"`"
SCRIPT_PATH="$(readlink "`basename "${SCRIPT_PATH}"`")";
done
cd "`dirname "${SCRIPT_PATH}"`" > /dev/null
SCRIPT_PATH="`pwd`";
popd > /dev/null
echo "srcipt=[${SCRIPT_PATH}]"
echo "pwd =[`pwd`]"
Known issuse
Script must be on disk somewhere, let it be over a network.
If you try to run this script from a PIPE it will not work
wget -o /dev/null -O - http://host.domain/dir/script.sh |bash
Technically speaking, it is undefined.
Practically speaking, there is no sane way to detect this.
Test case used
And the current test case that check that it works.
#!/bin/bash
# setup test enviroment
mkdir -p dir1/dir2
mkdir -p dir3/dir4
ln -s ./dir1/dir2/foo bar
ln -s ./../../dir3/dir4/test.sh dir1/dir2/foo
ln -s ./dir1/dir2/foo2 bar2
ln -s ./../../dir3/dir4/doe dir1/dir2/foo2
cp test.sh ./dir1/dir2/
cp test.sh ./dir3/dir4/
cp test.sh ./dir3/dir4/doe
P="`pwd`"
echo "--- 01"
echo "base =[${P}]" && ./test.sh
echo "--- 02"
echo "base =[${P}]" && `pwd`/test.sh
echo "--- 03"
echo "base =[${P}]" && ./dir1/dir2/../../test.sh
echo "--- 04"
echo "base =[${P}/dir3/dir4]" && ./bar
echo "--- 05"
echo "base =[${P}/dir3/dir4]" && ./bar2
echo "--- 06"
echo "base =[${P}/dir3/dir4]" && `pwd`/bar
echo "--- 07"
echo "base =[${P}/dir3/dir4]" && `pwd`/bar2
echo "--- 08"
echo "base =[${P}/dir1/dir2]" && `pwd`/dir3/dir4/../../dir1/dir2/test.sh
echo "--- 09"
echo "base =[${P}/dir1/dir2]" && ./dir1/dir2/test.sh
echo "--- 10"
echo "base =[${P}/dir3/dir4]" && ./dir3/dir4/doe
echo "--- 11"
echo "base =[${P}/dir3/dir4]" && ./dir3/dir4/test.sh
echo "--- 12"
echo "base =[${P}/dir3/dir4]" && `pwd`/dir3/dir4/doe
echo "--- 13"
echo "base =[${P}/dir3/dir4]" && `pwd`/dir3/dir4/test.sh
echo "--- 14"
echo "base =[${P}/dir3/dir4]" && `pwd`/dir1/dir2/../../dir3/dir4/doe
echo "--- 15"
echo "base =[${P}/dir3/dir4]" && `pwd`/dir1/dir2/../../dir3/dir4/test.sh
echo "--- 16"
echo "base s=[${P}]" && source test.sh
echo "--- 17"
echo "base s=[${P}]" && source `pwd`/test.sh
echo "--- 18"
echo "base s=[${P}/dir1/dir2]" && source ./dir1/dir2/test.sh
echo "--- 19"
echo "base s=[${P}/dir3/dir4]" && source ./dir1/dir2/../../dir3/dir4/test.sh
echo "--- 20"
echo "base s=[${P}/dir3/dir4]" && source `pwd`/dir1/dir2/../../dir3/dir4/test.sh
echo "--- 21"
pushd . >/dev/null
cd ..
echo "base x=[${P}/dir3/dir4]"
./`basename "${P}"`/bar
popd >/dev/null
PurpleFox aka GreenFox
Also note that homebrew's (http://brew.sh) coreutils package includes realpath (link created in/opt/local/bin).
$ realpath bin
/Users/nhed/bin
Using bash I suggest this approach. You first cd to the directory, then you take the current directory using pwd. After that you must return to the old directory to ensure your script does not create side effects to an other script calling it.
cd "$(dirname -- "$0")"
dir="$PWD"
echo "$dir"
cd - > /dev/null
This solution is safe with complex path. You will never have troubles with spaces or special charaters if you put the quotes.
Note: the /dev/null is require or "cd -" print the path its return to.
If you don't mind using perl:
ABSPATH=$(perl -MCwd=realpath -e "print realpath '$0'")
Can you try something like this inside your script?
echo $(pwd)/"$0"
In my machine it shows:
/home/barun/codes/ns2/link_down/./test.sh
which is the absolute path name of the shell script.
I've found this to be useful for symlinks / dynamic links - works with GNU readlink only though (because of the -f flag):
# detect if GNU readlink is available on OS X
if [ "$(uname)" = "Darwin" ]; then
which greadlink > /dev/null || {
printf 'GNU readlink not found\n'
exit 1
}
alias readlink="greadlink"
fi
# create a $dirname variable that contains the file dir
dirname=$(dirname "$(readlink -f "$0")")
# use $dirname to find a relative file
cat "$dirname"/foo/bar.txt
this is what I use, may need a tweak here or there
abspath ()
{
case "${1}" in
[./]*)
local ABSPATH="$(cd ${1%/*}; pwd)/${1##*/}"
echo "${ABSPATH/\/\///}"
;;
*)
echo "${PWD}/${1}"
;;
esac
}
This is for any file - and of curse you can just invoke it as abspath ${0}
The first case deals with relative paths by cd-ing to the path and letting pwd figure it out
The second case is for dealing with a local file (where the ${1##/} would not have worked)
This does NOT attempt to undo symlinks!
This works as long as it's not a symlink, and is perhaps marginally less ugly:
ABSPATH=$(dirname $(pwd -P $0)/${0#\.\/})
If you're using ksh, the ${.sh.file} parameter is set to the absolute pathname of the script. To get the parent directory of the script: ${.sh.file%/*}
I use the function below to emulate "readlink -f" for scripts that have to run on both linux and Mac OS X.
#!/bin/bash
# This was re-worked on 2018-10-26 after der#build correctly
# observed that the previous version did not work.
# Works on both linux and Mac OS X.
# The "pwd -P" re-interprets all symlinks.
function read-link() {
local path=$1
if [ -d $path ] ; then
local abspath=$(cd $path; pwd -P)
else
local prefix=$(cd $(dirname -- $path) ; pwd -P)
local suffix=$(basename $path)
local abspath="$prefix/$suffix"
fi
if [ -e $abspath ] ; then
echo $abspath
else
echo 'error: does not exist'
fi
}
# Example usage.
while (( $# )) ; do
printf '%-24s - ' "$1"
read-link $1
shift
done
This is the output for some common Mac OS X targets:
$ ./example.sh /usr/bin/which /bin/which /etc/racoon ~/Downloads
/usr/bin/which - /usr/bin/which
/bin/which - error: does not exist
/etc/racoon - /private/etc/racoon
/Users/jlinoff/Downloads - /Users/jlinoff/Downloads
The is the output for some linux targets.
$ ./example.sh /usr/bin/which /bin/whichx /etc/init.d ~/Downloads
/usr/bin/which - /usr/bin/which
/bin/whichx - error: does not exist
/etc/init.d - /etc/init.d
/home/jlinoff/Downloads - /home/jlinoff/Downloads