Monday, May 20, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1473  / 2 Years ago, mon, june 20, 2022, 4:26:40

I am new to the Linux environment. After a quick search I could not find a helpful question about the $ operator used in bash scripting. I do not know if this question relates more to the Ubuntu (or other) flavor of Linux, or my oh-so-small programming experience with it.



Basically I thought the ~ operand in Linux was perfectly equivalent to the string '/home/username'. If I try to execute a program after a certain delay by using a bash script and an external C-based program, I am so stuck: I cannot make use of the ~ operand and keep the program indifferent to the user (though the rest of the file path must be respected).



Please provide comments to the following (finding the getIdle sources is left to the reader):



#!/bin/bash
# call.sh

idle=false
idleAfter=30000 # consider idle after 30000 ms

# EXPLAIN THIS - Begin
# Strings that work
# str_getIdle_exe='/home/seb/Documents/Seb/Prjcts/Idle/getidle/src/getIdle'
# str_getIdle_exe="/home/seb/Documents/Seb/Prjcts/Idle/getidle/src/getIdle"

# Strings that don'work
str_getIdle_exe='~/Documents/Seb/Prjcts/Idle/getidle/src/getIdle'
# str_getIdle_exe="~/Documents/Seb/Prjcts/Idle/getidle/src/getIdle"
# EXPLAIN THIS - End


while true; do
idleTimeMillis= $str_getIdle_exe
echo $idleTimeMillis # just for debug purposes.
if [[ $idleTimeMillis -gt $idleAfter && $idle = false ]] ; then
echo "start idle" # or whatever command(s) you want to run...
idle=true
/usr/bin/xflock4
else # just for debug purposes.
echo "not there yet..."
fi

if [[ $idleTimeMillis -lt $idleAfter && $idle = true ]] ; then
echo "end idle" # same here.
idle=false
fi
sleep 1 # polling interval

done


What I am trying to explain is: why the two different strings above execute nicely in a shell, but not when called from a script?



I'm using Linux Mint 17.1 "Rebecca" Xfce (64-bit).



Many, many thanks.


More From » command-line

 Answers
3

The tilde is not expanded when in quotes -- ref: https://www.gnu.org/software/bash/manual/bashref.html#Tilde-Expansion



So you want to remove the quotes:



str_getIdle_exe=~/Documents/Seb/Prjcts/Idle/getidle/src/getIdle


If the path under your homedir contains a space, just leave the tilde unquoted:



some_path=~/"dir with spaces/file with spaces"

[#21563] Tuesday, June 21, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ainlyyor

Total Points: 210
Total Questions: 129
Total Answers: 116

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
;