Execute users default shell in a shell script

In a shell script I’d like to execute the users shell and continue the script once the shell finishes.

It should look something like this:

> myScript.sh
Script ouput
> echo "shell started by myScript.sh"
shell started by myScript.sh
> exit
More script output
>

It works when I execute a shell in the script:

echo "Script output"
bash
echo "More script output"

But I’d like it to use the users login shell (or the shell he was in before he started myScript.sh).

Go to Source
Author: Philippe

How to iterate over array indices in zsh?

In bash we can iterate over index of an array like this

~$ for i in "${!test[@]}"; do echo $i; done

where test is an array, say,

~$ test=(a "b c d" e f)

so that the output looks like

a
b c d
e
f

However, when I do the same in zsh I get an error:

➜ ~ for i in "${!test[@]}"; do echo $i; done
zsh: event not found: test[@]

What is going on?

What is the proper way of iterating over indices in zsh?

Go to Source
Author: Gao Zheng

Return “continue” from function called from loop

I’m currently refactoring a script which has slowly grown beyond control. I’m trying to spin off repetition into functions. However, I have a repeated test being called from a loop, and want it to return continue.

Shellcheck says

SC2104: In functions, use `return` instead of `continue`.

And the shellcheck wiki says don’t do it. But is there a way?

Below is an example:

#!/bin/sh

AFunction () {
    if [[ "${RED}" -lt 3 ]]; then
        echo "cont"
        continue
    else
        echo "nope"
    fi
}

for i in 1 2 3 4 5
do
    RED=${i}
    AFunction
    echo ${i}
done

This is the output:

cont
1
cont
2
nope
3
nope
4
nope
5

But I would expect

cont
cont
nope
3
nope
4
nope
5

Go to Source
Author: Stripy42

expect script execute and then detach from child process

I am using expect script to switch to sudo user and then run some custom commands. I am using zsh + Tilix terminal.

#!/usr/bin/expect

set timeout 60
spawn sudo su
expect ": "
sleep .5
send "passwordr"
expect "u0007"
sleep .5
send "cd /path/to/a/folder && source some_scriptr"
interact

As I understand, everything I type after interact instruction runs as a child process of the expect process. This causes some weird autocompletion problem in zsh configuration while copy-pasting a long command. The history navigation with up or down arrow keys causes the command overlapping e.g. when I type the command

echo "Wikipedia is a multilingual online encyclopedia created and maintained as an open collaboration project by a community of volunteer editors using a wiki-based editing system." 

and pressed the up-arrow key, the resulting command looks like
enter image description here

The above problem does not occur when I manually enter the expect script commands.

So, Is there any way to execute expect script with switching user and detach from expect process completely? Or any idea on resolving the above problem?

Go to Source
Author: Mohit Kumar Jangid

SSH Time out Error

I am looking for new ideas on how can I do more better.

Home laptop (home Laptop can access only Linux VM)
Linux VM (This machine can access only jump box)
jump box VM

I can ssh fine from Linux VM to jump box keys are setup.

one user is set up in Linux VM called joe and Joe .bash_profile looks like this.

ssh 10.0.0.1 || ssh 10.0.0.2

Note: There is two nic on jump box if one is down we can use the other nic for login.

Let’s say first nic is down and when I do telnet from my home laptop (telnet Linux VM) and provide user name joe it should automatically connect us to 10.0.0.1 but one is down it’s giving message timeout and taking a long time to connect to other nic.

is there any way when I do telnet and give user name check first ssh connection if down automatically connects to other second one like in 2-3 secs?

Right now it will give us a message

Time out
time out
time out
And then it will try to connect the second nic.

I can more clarify if you guys have any more questions for me.

Go to Source
Author: John

ANSWER

Resolving hosts can add up to the connection attempt, so specifying an IP address directly can shave off some of those precious milliseconds. If it’s only in a local network with the IP addresses all laid out that should not be the case. At least that’s what I think.

Perhaps explicitly setting the ConnectTimeout option to a shorter one. Like so,

ssh -o ConnectTimeout=10 user@host

This can help. Adjust the number to a sweet spot that works for your ssh setup as necessary.

Fix Syntax error: unexpected end of file in a “for loop”

So I tried to fix this problem by myself for a couple of hours but I couldn’t
here’s the code block that has a problem I changed it, rewritten, I even tried to “do” only echo after each line but it doesn’t seem to work. It is a code to decrypt CSV files of a game for some event thing. In case u want, here’s full code https://pastebin.com/Gv3Fvyxy

for path in $(find assets -name "*.csv")

    do 

    f=$(echo "$path" | rev | cut -d"/" -f1 | rev)

        (

            dd if=$path bs=1 count=9 status=none

            dd if=/dev/zero bs=1 count=4 status=none

            dd if=$path bs=1 skip=9 status=none

        ) | lzma -dc -f > "decrypted/"$f

    done

Go to Source
Author: Dev

is there a better way to search for shell-script commands in google

Many times , i was stuck in shell script commands and couln’t even find it on google, when i directly type them in the search.Is there any way to search for , or any other reference site where i can get most information on the commands.
could you please explain what these does.

1) -inv ${SITE}

2) ${first}/* ${second}

Go to Source
Author: nikhil2000

how to append a string at the end of a line but also before some n characters

  • List item

how to add some string at the end of the line in a file : But also before 3 charcters .

I tried but ended up by appending at the last of the string .can anyone help me pls

Example :
I/P:
file1.txt:

content :
line1 :a
line2 :bbbb
lin3: vvvvv
line4: cccccc
line5: dd
line6 :rules(‘test’, ‘.*’, [abc,bcd]).

i tried using
sed -i ‘/rules/ s/$/ ***/’ file1.txt

O/P:
rules(‘test’,[abc,bcd]). ***

But preffered o/p:
rules(‘test’,[abc,bcd,***]).

Go to Source
Author: yugandhar

I was installing Arch Linux but I messed up, I wanted to restart so I rebooted and booted back into my bootable USB. I am now in the interactive shell

Like I mentioned in the title I was at point where I was about to install grub but I found out I had messed up so I restarted and booted back into my USB, I am now stuck in the interactive shell. I am going to try to reboot and boot into manjaro using anothet USB unless someone says I shouldn’t.

Here’s a picture of my situation http://imgur.com/gallery/0kCyyxr

Go to Source
Author: probablyasmurf