Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

0 votes
1 answers
47 views
bash conditional variable assignment cli not what I expected
Can anyone explain what's going on with DR here? ``` user@host:~# DRYRUN=true user@host:~# echo "$DRYRUN" true user@host:~# $DRYRUN && export DR="-n" user@host:~# echo $DR user@host:~# $DRYRUN && export DR="-n" && echo $DR user@host:~# $DRYRUN && export DR="-n" && echo "$DRYRUN --- $DR" true --- -n...
Can anyone explain what's going on with DR here?
user@host:~# DRYRUN=true
user@host:~# echo "$DRYRUN"
true
user@host:~# $DRYRUN && export DR="-n"
user@host:~# echo $DR
user@host:~# $DRYRUN && export DR="-n" && echo $DR
user@host:~# $DRYRUN && export DR="-n" && echo "$DRYRUN --- $DR"
true --- -n
user@host:~# echo $DR
user@host:~# echo "$DRYRUN --- $DR"
true --- -n
user@host:~#
I Just want to assign DR to the string "-n" when DRYRUN is set to true but what I'm getting is confusing
Hugh (19 rep)
May 12, 2025, 03:04 PM • Last activity: May 12, 2025, 03:43 PM
20 votes
3 answers
103480 views
Zsh: export: not valid in this context
When running [this script](https://gist.github.com/amelio-vazquez-reina/0befbd311e96f8787754), I run into an error on [this line][1] (relevant snippet below): ... _NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@") if (( $? == 0 )); then export PATH=$_NEW_PATH # If the string contains / it's a path if [...
When running [this script](https://gist.github.com/amelio-vazquez-reina/0befbd311e96f8787754) , I run into an error on this line (relevant snippet below): ... _NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@") if (( $? == 0 )); then export PATH=$_NEW_PATH # If the string contains / it's a path if [[ "$@" == */* ]]; then export CONDA_DEFAULT_ENV=$(get_abs_filename "$@") else export CONDA_DEFAULT_ENV="$@" fi # ==== The next line returns an error # ==== with the message: "export: not valid in this context /Users/avazquez/anaconda3" export CONDA_ENV_PATH=$(get_dirname $_THIS_DIR) if (( $("$_THIS_DIR/conda" ..changeps1) )); then CONDA_OLD_PS1="$PS1" PS1="($CONDA_DEFAULT_ENV)$PS1" fi else return $? fi ... Why is that? I found this ticket , but I don't have that syntax error. I found reports of the same problem in GitHub threads (e.g. here ) and mailing lists (e.g. here )
Amelio Vazquez-Reina (42851 rep)
Jun 10, 2015, 02:11 AM • Last activity: Apr 18, 2025, 07:14 PM
-3 votes
1 answers
78 views
Need working examples of using tilde expansion immediately following the : (colon) sign in variable assignment
In the Bash manual, it is written about the tilde expansion: > Each variable assignment is checked for unquoted tilde-prefixes > immediately following a ‘:’ or the first ‘=’. [Read the Bash manual about tilde expansion.][1] [1]: https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html...
In the Bash manual, it is written about the tilde expansion: > Each variable assignment is checked for unquoted tilde-prefixes > immediately following a ‘:’ or the first ‘=’. Read the Bash manual about tilde expansion. I assumed that the : sign when assigning a variable is related to parameter expansion and instructed one of the popular AIs to find examples that satisfy the conditions defined in the Bash manual. The summary of his response is as follows: - There isn't a valid working example with the tilde immediately following the : without additional characters.
Kiki Miki (27 rep)
Feb 12, 2025, 12:07 PM • Last activity: Feb 13, 2025, 04:15 AM
0 votes
2 answers
136 views
variable assignment doesn't create one same object at least for grep
The problem is as follows (Here I don't use [`find`][1] since it doesn't support double-asterisk wildcard `**`): ```bash $ FILES=(foo/**/*.suffix bar/**/*.suffix2) $ grep baz "${FILES[@]}" # works # I use this to create one local var with local in one function $ SUBFILES="${FILES[@]}" $ grep baz "${...
The problem is as follows (Here I don't use find since it doesn't support double-asterisk wildcard **):
$ FILES=(foo/**/*.suffix bar/**/*.suffix2)
$ grep baz "${FILES[@]}" # works
# I use this to create one local var with local in one function
$ SUBFILES="${FILES[@]}"
$ grep baz "${SUBFILES[@]}" # doesn't work
I used od to check them but they are same at least have the same number of "\n" and the same length (I didn't check character by character. But at a glance they are same).
$ echo ${SUBFILES[@]} | od -c
$ echo ${FILES[@]} | od -c
Why does the variable assignment not create one object still work for grep?
An5Drama (173 rep)
Jan 2, 2025, 08:43 AM • Last activity: Jan 2, 2025, 06:52 PM
-1 votes
1 answers
471 views
Zsh string variable containing multi words must be assigned to an array
How to have Zsh string variable containing multi words separated by space be assigned to an array so that each word is the array element s='run help behind me' a=($s) m=${a[0]} n=${a[1]} print "m=$m" echo n=$n m= n=run help behind me confusing, so simple but still cannot do it so.. Really please hel...
How to have Zsh string variable containing multi words separated by space be assigned to an array so that each word is the array element s='run help behind me' a=($s) m=${a} n=${a} print "m=$m" echo n=$n m= n=run help behind me confusing, so simple but still cannot do it so.. Really please help out
user571604
May 15, 2023, 09:56 AM • Last activity: May 15, 2023, 07:19 PM
1 votes
1 answers
125 views
How to iterate over a directory for files without including sub-directories in Bash
I need to parse through a directory that contains files and subdirectories however I am unable to use the find command. The for loop is supposed to go through each file and compare it to another directory but I cannot get it to pass over the subdirectories. difference=0 for item in $1 do if [[ -d $i...
I need to parse through a directory that contains files and subdirectories however I am unable to use the find command. The for loop is supposed to go through each file and compare it to another directory but I cannot get it to pass over the subdirectories. difference=0 for item in $1 do if [[ -d $item ]] then continue fi if [ ! -f "$2/$(basename $item)" ] then echo "$2/$(basename $item) is missing" difference=1 fi if [ diff $item "$2/$(basename $item)" ] then echo "$1/$(basename $item) is different" difference=1 fi done Using the code above it does skip over the subdirectories but then also doesn't compare the files. I know that if I omit the first if statement with the continue and put: for item in $(find $1 -type f) It works however I am unable to use that function. Any help is greatly appreciated. Furthermore I also have the other directory comparing to this one since its the same code I didn't write it down.
IMPNick (11 rep)
Feb 13, 2023, 01:26 AM • Last activity: Feb 13, 2023, 02:28 AM
9 votes
2 answers
797 views
In POSIX scripting, is x=$y always equivalent to x="$y"?
Are x=$y and x="$y" always equivalent? Didn't know how to search for this. So far, I've always been using `x="$y"` to be "on the safe side". But I used `x=$1` at one point and noticed that, apparently, I don't even need the extra double quotation marks. Where is the behavior defined in The Open Grou...
Are x=$y and x="$y" always equivalent? Didn't know how to search for this. So far, I've always been using x="$y" to be "on the safe side". But I used x=$1 at one point and noticed that, apparently, I don't even need the extra double quotation marks. Where is the behavior defined in The Open Group POSIX document?
finefoot (3554 rep)
Oct 4, 2022, 11:42 PM • Last activity: Oct 6, 2022, 05:07 AM
-1 votes
1 answers
59 views
Grouping commands of if...then statement, including assignation of variable for shell/bash in a single line
I know the existence of *[while-do-done][1]*. Alternatively, I know that I can use *[for-do-done][2]*. But, I was trying to test manually using *if-then-else*. I would to like to execute a command, but doing a shift with addition in each iteration. sh-3.2# currPos=0;stepSize=16; sh-3.2# hexdump -n $...
I know the existence of *while-do-done *. Alternatively, I know that I can use *for-do-done *. But, I was trying to test manually using *if-then-else*. I would to like to execute a command, but doing a shift with addition in each iteration. sh-3.2# currPos=0;stepSize=16; sh-3.2# hexdump -n $stepSize -Cv /dev/disk0s1 -s $currPos;currPos=$(($currPos + $stepSize));echo $currPos; 00000000 8d 1c 09 48 65 8c 3c 6e 01 00 00 00 00 00 00 00 |...He.offset (-s) is not taking the updated value of variable $currPos. But in the echo command the $currPos variable is changing (16, 32 and 48)! **EDIT 1**: According to *Kusalananda* (Thanks a lot!) I fix the first step. sh-3.2# currPos=0;stepSize=16;finalStep=48; sh-3.2# hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; 00000000 8d 1c 09 48 65 8c 3c 6e 01 00 00 00 00 00 00 00 |...He.https://linuxize.com/post/bash-if-else-statement/ something of: if, then, else, fi and (-eq, -gt, -ge, -lt, -le, !=) and break. Trying to use a iterative with if, break: sh-3.2# currPos=0;stepSize=16;finalStep=48; sh-3.2# if [ $currPos -le $finalStep ]; then (hexdump -n $stepSize -Cv /dev/disk0s1 -s $currPos;currPos=$(($currPos + $stepSize));echo $currPos;); else break; fi 00000000 8d 1c 09 48 65 8c 3c 6e 01 00 00 00 00 00 00 00 |...He.Note**: I'm grouping inside of ( and ) the commands (hexdump, assignation and echo) of the then of the if statement. But, in this case, the echo is working worst the first case (the value of $currPos variable is not changed). According to this article I need to use { and }. But I get: sh-3.2# if [ $currPos -le $finalStep ]; then {hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos;}; else break; fi sh: syntax error near unexpected token `}' **How I can do that?, How solve this (the update value of variable is not taken)?** **EDIT 2** *Stéphane-Chazelas* Helped me! (read comments about { and }, note the space after of { and before of } respectively). sh-3.2# currPos=0;stepSize=16;finalStep=48; sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; }; else break; fi 00000000 8d 1c 09 48 65 8c 3c 6e 01 00 00 00 00 00 00 00 |...He.else break): sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos }; else break; fi sh: syntax error near unexpected token `else' sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; }; else break; fi sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; } else break; fi sh-3.2#
joseluisbz (375 rep)
Aug 15, 2022, 05:58 AM • Last activity: Aug 15, 2022, 08:02 AM
0 votes
1 answers
719 views
Assigning $* in shell script from args array
After iterating thru the shell arguments and filtering out unwanted arguments into an array, I want to assign the array back to `$*`. For example, in the below script, I want to remove the arguments `-f java`, which are in sequence, and store the rest of the arguments in the `args` array. Is there a...
After iterating thru the shell arguments and filtering out unwanted arguments into an array, I want to assign the array back to $*. For example, in the below script, I want to remove the arguments -f java, which are in sequence, and store the rest of the arguments in the args array. Is there a way to assign the filtered arguments back to $*? I am trying to do set --${*} $args, but it is taking only the first argument and not the whole array. If I pass
$ .test.sh arg1 arg2 -f java7 arg3 
arg1 arg2 agr3
while (( $# )); do
    if [[ $1 = "-f" ]] && [[ $2 = "java7" ]]; then
        unset USE_JAVA8
        JAR=signals.jar
        echo "USE_JAVA8 Flag is OFF running Signal on java7"
        shift 2
        continue
    fi

    args+=( "$1" )
    shift
done


set --${*} $args

echo $*
user3118280 (15 rep)
Jul 18, 2022, 03:37 AM • Last activity: Jul 18, 2022, 06:27 AM
42 votes
3 answers
21614 views
Spaces in variable assignments in shell scripts
What is the difference between below variables assignments? var=23 var =23 var= 23 var = 23 Is there any difference in space around the assignment operator?
What is the difference between below variables assignments? var=23 var =23 var= 23 var = 23 Is there any difference in space around the assignment operator?
ajay (471 rep)
Jan 30, 2016, 01:20 PM • Last activity: Jun 29, 2022, 01:54 PM
10 votes
5 answers
11328 views
Assign the same string to multiple variables
I want to assign the string contained in `$value` to multiple BASH variables. Actually the example I gave before (`var1=var2=...=$value`) was not reflecting exactly what I wanted. So far, I found this but it only works if `$value` is an integer: $ let varT=varz=var3=$value How can I do that?
I want to assign the string contained in $value to multiple BASH variables. Actually the example I gave before (var1=var2=...=$value) was not reflecting exactly what I wanted. So far, I found this but it only works if $value is an integer: $ let varT=varz=var3=$value How can I do that?
SebMa (2433 rep)
May 15, 2019, 07:06 AM • Last activity: Apr 28, 2022, 03:58 PM
1 votes
0 answers
1392 views
Completely disable NetworkManager from using DHCP client?
I am using RHEL 7.9. This version of RHEL I use `nmcli` and `nmtui` to configure network connections. Problem: NetworkManager service appears to be filling up journalctl logs with the following output ``` Apr 24 02:16:03 taxmd-trex-02 NetworkManager[1431]: [1650780963.7165] device (p2p3_2): Activati...
I am using RHEL 7.9. This version of RHEL I use nmcli and nmtui to configure network connections. Problem: NetworkManager service appears to be filling up journalctl logs with the following output
Apr 24 02:16:03 taxmd-trex-02 NetworkManager:   [1650780963.7165] device (p2p3_2): Activation: starting connection 'p2p3_2' (49fb6e36-468f-4a5b-8015-4d6e7728f82c)
Apr 24 02:16:03 taxmd-trex-02 NetworkManager:   [1650780963.7167] device (p2p3_4): Activation: starting connection 'p2p3_4' (673b8472-90f0-4e7d-ace2-91f912c94b1d)
Apr 24 02:16:03 taxmd-trex-02 NetworkManager:   [1650780963.7168] device (p2p3_2): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:03 taxmd-trex-02 NetworkManager:   [1650780963.7172] device (p2p3_4): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:03 taxmd-trex-02 NetworkManager:   [1650780963.7176] device (p2p3_2): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:03 taxmd-trex-02 NetworkManager:   [1650780963.7194] device (p2p3_4): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:03 taxmd-trex-02 NetworkManager:   [1650780963.7212] device (p2p3_2): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:03 taxmd-trex-02 NetworkManager:   [1650780963.7221] device (p2p3_4): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7132] dhcp4 (p2p3): request timed out
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7132] dhcp4 (p2p3): state changed unknown -> timeout
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7214] dhcp4 (p2p3): canceled DHCP transaction, DHCP client pid 78427
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7214] dhcp4 (p2p3): state changed timeout -> done
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7217] device (p2p3): state change: ip-config -> failed (reason 'ip-config-unavailable', sys-iface-state: 'managed')
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7222] device (p2p3): Activation: failed for connection 'p2p3'
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7224] device (p2p3): state change: failed -> disconnected (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7329] policy: auto-activating connection 'p2p3' (0af065ee-a8a0-997a-c03a-b737387d1a9f)
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7333] device (p2p3): Activation: starting connection 'p2p3' (0af065ee-a8a0-997a-c03a-b737387d1a9f)
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7333] device (p2p3): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7337] device (p2p3): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7434] device (p2p3): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7437] dhcp4 (p2p3): activation: beginning transaction (timeout in 45 seconds)
Apr 24 02:16:06 taxmd-trex-02 NetworkManager:   [1650780966.7448] dhcp4 (p2p3): dhclient started with pid 78439
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7124] dhcp4 (p2p4): request timed out
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7124] dhcp4 (p2p4): state changed unknown -> timeout
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7206] dhcp4 (p2p4): canceled DHCP transaction, DHCP client pid 78433
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7206] dhcp4 (p2p4): state changed timeout -> done
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7207] device (p2p4): state change: ip-config -> failed (reason 'ip-config-unavailable', sys-iface-state: 'managed')
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7211] device (p2p4): Activation: failed for connection 'p2p4'
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7213] device (p2p4): state change: failed -> disconnected (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7319] policy: auto-activating connection 'p2p4' (8cd96d38-f953-c130-f247-05b56972a953)
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7321] device (p2p4): Activation: starting connection 'p2p4' (8cd96d38-f953-c130-f247-05b56972a953)
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7322] device (p2p4): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7325] device (p2p4): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7422] device (p2p4): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7424] dhcp4 (p2p4): activation: beginning transaction (timeout in 45 seconds)
Apr 24 02:16:07 taxmd-trex-02 NetworkManager:   [1650780967.7435] dhcp4 (p2p4): dhclient started with pid 78445
Apr 24 02:16:08 taxmd-trex-02 NetworkManager:   [1650780968.7125] policy: auto-activating connection 'p2p4_1' (ff70a4ab-0b4b-4558-bf07-e24aa8319e72)
Apr 24 02:16:08 taxmd-trex-02 NetworkManager:   [1650780968.7129] device (p2p4_1): Activation: starting connection 'p2p4_1' (ff70a4ab-0b4b-4558-bf07-e24aa8319e72)
Apr 24 02:16:08 taxmd-trex-02 NetworkManager:   [1650780968.7130] device (p2p4_1): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:08 taxmd-trex-02 NetworkManager:   [1650780968.7134] device (p2p4_1): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:08 taxmd-trex-02 NetworkManager:   [1650780968.7154] device (p2p4_1): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:13 taxmd-trex-02 NetworkManager:   [1650780973.7115] device (p2p3_5): state change: ip-config -> failed (reason 'ip-config-unavailable', sys-iface-state: 'managed')
Apr 24 02:16:13 taxmd-trex-02 NetworkManager:   [1650780973.7119] device (p2p3_5): Activation: failed for connection 'p2p3_5'
Apr 24 02:16:13 taxmd-trex-02 NetworkManager:   [1650780973.7122] device (p2p3_5): state change: failed -> disconnected (reason 'none', sys-iface-state: 'managed')
Apr 24 02:16:13 taxmd-trex-02 NetworkManager:   [1650780973.7150] policy: auto-activating connection 'p2p3_5' (a70daadb-15be-4233-807e-3a09e94fdd3e)
This repeats for all interfaces without an ip address assigned. I think this is dhcp related. I do not want any dhcp client to be running on any interface. Some of these interfaces will have static addresses, others will not. It seems the interfaces that do not have an IP address, NetworkManager by default uses auto configuration and tries to get DHCP. I can assign link-local, and it will grab an APIPA address, I dont want that either. Is it possible to just have an enabled interface with NO ip address. And make NetworkManager stop trying to get an IP?
Dave (700 rep)
Apr 25, 2022, 03:59 PM
7 votes
2 answers
598 views
why is zsh not expanding this glob defined inside a function '(:a)'
The second line of the script only works if I trigger glob expansion by executing `echo`. I can't understand why. Here's the command and it's execution to give some context. Function definition: ``` ~/ cat ~/.zsh/includes/ascii2gif ascii2gif () { setopt extendedglob input=$(echo ${1}(:a)) _path=${in...
The second line of the script only works if I trigger glob expansion by executing echo. I can't understand why. Here's the command and it's execution to give some context. Function definition:
~/ cat ~/.zsh/includes/ascii2gif
ascii2gif () {
        setopt extendedglob
        input=$(echo ${1}(:a))
        _path=${input:h}
        input_f=${input:t}
        output_f=${${input_f}:r}.gif
        cd $_path
        nerdctl run --rm -v $_path:/data asciinema/asciicast2gif -s 2 -t solarized-dark $input_f $output_f
}
Activate function debugging for ascii2gif function..
~/ typeset -f -t ascii2gif
Debugged function execution:
~/ ascii2gif ./demo.cast
+ascii2gif:1> input=+ascii2gif:1> echo /Users/b/demo.cast
+ascii2gif:1> input=/Users/b/demo.cast
+ascii2gif:2> _path=/Users/b
+ascii2gif:3> input_f=demo.cast
+ascii2gif:4> output_f=demo.gif
+ascii2gif:5> cd /Users/b
+_direnv_hook:1> trap -- '' SIGINT
+_direnv_hook:2> /Users/b/homebrew/bin//direnv export zsh
+_direnv_hook:2> eval ''
+_direnv_hook:3> trap - SIGINT
+ascii2gif:6> nerdctl run --rm -v /Users/b:/data asciinema/asciicast2gif -s 2 -t solarized-dark demo.cast demo.gif
==> Loading demo.cast...
==> Spawning PhantomJS renderer...
==> Generating frame screenshots...
==> Combining 40 screenshots into GIF file...
==> Done.
I've tried numerous variations to try and force expansion such as input=${~1}(:a) etc, but to no avail. Any suggestions? Obviously the script works but seems sub-optimal.
bryan hunt (73 rep)
Mar 30, 2022, 11:48 AM • Last activity: Mar 30, 2022, 08:51 PM
2 votes
1 answers
272 views
How can I run speech to text and save the result in a variable?
I would like to speak into my computer's microphone, have what I say converted to text and then have that available as a shell variable. Is this possible? I thought I might do it using Google's speech input feature: [![enter image description here][1]][1] [1]: https://i.sstatic.net/YMpLk.png
I would like to speak into my computer's microphone, have what I say converted to text and then have that available as a shell variable. Is this possible? I thought I might do it using Google's speech input feature: enter image description here
venkatraman (21 rep)
Dec 29, 2015, 08:24 AM • Last activity: May 27, 2021, 06:16 PM
8 votes
2 answers
4809 views
changing IFS temporarily before a for loop
I know that the `SHELL` allows variable assignment to take place immediately before a command, such that `IFS=":" read a b c d <<< "$here_string"` works... What I was wondering is do such assignments not work when done with compound statements such as loops? I tried something like `IFS=":" for i in...
I know that the SHELL allows variable assignment to take place immediately before a command, such that IFS=":" read a b c d <<< "$here_string" works... What I was wondering is do such assignments not work when done with compound statements such as loops? I tried something like IFS=":" for i in $PATH; do echo $i; done but it results in a syntax error. I could always do something like oldIFS="$IFS"; IFS=":"; for....; IFS="$oldIFS", but I wanted to know if there was any way I could make such inline assignments work for compound statements like for loops?
computronium (878 rep)
Aug 16, 2020, 01:32 PM • Last activity: Mar 16, 2021, 07:02 PM
0 votes
2 answers
2098 views
How to move home using relative path
The question is "Your current directory is cambridge. Move to your home directory using a relative pathname (don't use cd by itself for this move)" but I cannot for the life of me figure it out.. I tried everything. [![enter image description here][1]][1] [1]: https://i.sstatic.net/wCv0f.png
The question is "Your current directory is cambridge. Move to your home directory using a relative pathname (don't use cd by itself for this move)" but I cannot for the life of me figure it out.. I tried everything. enter image description here
AASFLC (3 rep)
Jan 20, 2021, 07:29 PM • Last activity: Jan 20, 2021, 10:28 PM
51 votes
2 answers
13190 views
Are quotes needed for local variable assignment?
Can I safely omit quotes on the right side of a local assignment? function foo { local myvar=${bar} stuff() } I'm mainly interested in `bash`, but any info on corner cases in other shells are welcome.
Can I safely omit quotes on the right side of a local assignment? function foo { local myvar=${bar} stuff() } I'm mainly interested in bash, but any info on corner cases in other shells are welcome.
rahmu (20511 rep)
Oct 25, 2013, 01:17 PM • Last activity: Dec 16, 2020, 06:54 AM
0 votes
0 answers
95 views
Variable assignment echo $VAR in subshell is a good method or not? (Nested bash functions)
I'm not a BASH programmer and whenever I have to create scripts it's a lottery for me, so apologies if my script looks too redudant or written by a newbie. As a foreword I have many screen snapshots taken on a Windows machine where the two display are captured. I organized them in subfolders and I w...
I'm not a BASH programmer and whenever I have to create scripts it's a lottery for me, so apologies if my script looks too redudant or written by a newbie. As a foreword I have many screen snapshots taken on a Windows machine where the two display are captured. I organized them in subfolders and I want to take a general approach to crop images in them. As an aside, since I don't like filename as Windows generates them, i.e. with spaces and parenthesys, I want to remove such characters. All above is just one of many cases where I may have to apply actions to files residing in subfolders. I organized the script by actions to be taken on files. Now to the point: in wring the script I wanted it to be general purpose and hene I tried to have functions behaving like other scripting languages trying not to use global variables if not stricly necessary. Now the point: since I have to assign strings to variables eventually I was forced to use "echo -n" in a subshell I read opposite comments on usign echo in a subshell to assign a string made of spaces or other special characters to a variable. I tested one by one and on their own the three functions here below and they worked. That means that when required I forced values in the function itself. As soon as I nested them in the third one the only thing I know is that if I didn't use this tecnique of echoing the variable in a subshell and capturing the output it would never have worked: except for a variable, explained at the bottom. trim_weird_char () { # set +x local _bn="$(basename "$1")" # echo "[twc bn] $_bn" local _dn="$(dirname "$1")" # echo "[twc dn] $_dn" local _fn="$(echo -n $_bn | tr -d ' ' | tr -d '(' | tr -d ') ')" # echo "[twc fn] $_fn" local _ifn="$(echo -n "${_dn}"/"${_bn}")" local _dfn="$(echo -n "${_dn}"/"${_fn}")" mv "$_ifn" "$_dfn" echo "$_dfn" # set -x } crop_img() { # set +x #$1 = image #$2 = resize #$3 = string to add in the output filename before the extension local _img="$(echo -n "$1")"; # we want the file name and hence we remove the extension by NOT being GREEDY with the removal local _fn="$(echo -n "${_img%.*}")" local _ext="$(echo -n "${_img##*.}")"; # we want the extension and hence we remove what comes before by being GREEDY with the removal local _dfn="$(echo -n "${_fn}${3}.${_ext}")" convert "$_img" -crop "$2" "$_dfn" echo "$(echo -n "$_dfn")" } crop_smartbe () { # set -x # ls -la | awk '{print $5}' echo -n "Trimming name for $1..." local _img="$(trim_weird_char "$1")" echo "done: $_img" echo -n "Cropping $_img to... " local _s="$(ls -la "$_img" | awk '{print $5}')" local _dfn="$(echo -n $(crop_img "$_img" "1080x1920+0+0" "_cropped"))" local _s_img=$(ls -la "$_dfn" | awk '{print $5}') echo "$_dnf done: size from $_s to $_s_img" } A filename I tested my functions on is the following (I source'd the script above) gadger@brunas:/brunas/expenses/2020/Demandes de reimbursement/Demandes$ crop_smartbe Full/1261597\ -\ Frais\ Internet\ -\ 1er\ sémestre/Screenshot\ \(142\).png Trimming name for Full/1261597 - Frais Internet - 1er sémestre/Screenshot (142).png...done: Full/1261597 - Frais Internet - 1er sémestre/Screenshot142.png Cropping Full/1261597 - Frais Internet - 1er sémestre/Screenshot142.png to... done: size from 292631 to 100694 For this (in crop_smartbe) local _dfn="$(echo -n $(crop_img "$_img" "1080x1920+0+0" "_cropped"))" ... echo "$_dnf done: size from $_s to $_s_img" still $_dnf is not assigned correctly even though I used the echo -n tecnique. Taken for granted that removing the echo function breaks the script, shall I prefer printf instead? Or should I rewrite the script in a different manner that doesn't require the use of echo? In case how? Thanks Alex
Alex (21 rep)
Dec 6, 2020, 10:15 PM • Last activity: Dec 6, 2020, 10:49 PM
0 votes
1 answers
746 views
one line definition of a variable conditional on the output of a logical test
In `bash` programming I've always defined conditional variables in the following long hand way dog=1 if [[ $dog -eq 1 ]] ; then cow=1 else cow=0 fi This is obviously incredibly inefficient. In `MATLAB` I can do the following cow=(dog==1) i.e., one line instead of five. Is there an analogous way to d...
In bash programming I've always defined conditional variables in the following long hand way dog=1 if [[ $dog -eq 1 ]] ; then cow=1 else cow=0 fi This is obviously incredibly inefficient. In MATLAB I can do the following cow=(dog==1) i.e., one line instead of five. Is there an analogous way to do this in bash? Thanks for any advice.
Leo Simon (453 rep)
Aug 21, 2020, 06:01 PM • Last activity: Aug 22, 2020, 12:32 AM
1 votes
1 answers
1103 views
Result of a command is not assigned to a variable
I am pretty unexperienced (to put it mildly) when it comes to BASH and Shell scripting, so bear with me: $ toda=$(date) | echo $toda gives me: Fr 29 Mai 2020 15:25:19 CEST. So far so good. datediff is part of the dateutils package and gives back the number of days between to dates: datediff 2019-12-...
I am pretty unexperienced (to put it mildly) when it comes to BASH and Shell scripting, so bear with me: $ toda=$(date) | echo $toda gives me: Fr 29 Mai 2020 15:25:19 CEST. So far so good. datediff is part of the dateutils package and gives back the number of days between to dates: datediff 2019-12-31 2020-05-29 gives me: 150. Again, so far so good. But: toda=$(datediff 2019-12-31 2020-05-29) | echo $toda gives me back: Fr 29 Mai 2020 15:25:19 CEST and not (as expected) 150. In other words it did not assign the datediff result but kept the value from the former operation unchanged. Of course I tried: anothervarname=$(datediff 2019-12-31 2020-05-29) | echo $anothervarname which gives back an empty variable (ie a blank line above the prompt). What do I have to do to assign the datediff result from the above example to a variable? Thanks for your help.
Guebert (13 rep)
May 29, 2020, 01:50 PM • Last activity: May 29, 2020, 01:53 PM
Showing page 1 of 20 total questions