我正试图通过bash脚本来自动化我的日常工作。为此,我需要创建以下逻辑脚本
从根用户切换到另一个linux用户打开指定路径
这是我的代码logic.sh
#! /bin/bash
su another_user
cd /home/another_user #tried with sudo too
logic.sh执行后
. logic.sh
我看到root用户切换到了/root,但我仍然停留在/root目录中。
我做错了什么或者bash脚本不适合于用户切换逻辑中的自动化逻辑
我是shell脚本的初学者。我正试图用脚本打开4终端。这是密码
for i in 1 2 3 4
do
echo $i
if [ $i -eq 1 ]; then
# xterm -e
xterm -e "source /home/akshay/openapps.sh; bash"
opensublime
elif [ $i -eq 2 ]; then
xterm -e "source /home/akshay/openapps.sh; bash"
open
我正在我的bash脚本(比如child.sh)中调用bash脚本(比如parent.sh),我希望脚本(child.sh)中的错误被困在我的父脚本(parent.sh)中。
我通过和阅读。基于此,我认为我应该对我的父脚本执行set -E,以便由子shell继承这些陷阱。因此,我的代码如下
parent.sh
#!/bin/bash
set -E
error() {
echo -e "$0: \e[0;33mERROR: The Zero Touch Provisioning script failed while running the command $BASH_COMM
我正在尝试让emacs认识到脚本(应该是源代码)是用bash编写的。我把
# this is a -*- bash -*- script
在第一行上,但它仍然在sh模式下打开。我可以通过将#!/usr/bin/env bash放在第一行来让它工作,但我不愿意这样做,因为它使脚本看起来像是可执行的,而我希望它是源代码。为什么emacs不能识别我的提示?
关于使用pgrep搜索哪些shell进程正在运行与当前脚本相同的脚本,这里有一个非常奇怪的问题。
下面是名为test.sh的测试脚本
#!/bin/bash
full_res=`pgrep -a -l -f 'test\.sh'`
res=$(pgrep -a -l -f 'test\.sh' | cat)
echo "short result is $full_res"
echo "weird result is $res"
有输出
sh test.sh &
[1] 19992
➜ logs short res