我正在编写一个脚本,它允许我查看整个选定目录中每个文件的内容,但我不太知道该怎么做。
我在想的输出通过管道ls进入cat或less但这似乎并没有工作时。
是否有我需要使用的命令行变量,或者是否有满足我需要的命令?
我想要这个,这样每当我在寻找某个我不记得的文件时,我就可以快速查看文件的内容。
大家,我有这个脚本,到目前为止我一直使用没有任何问题,它使用两个文件来创建一个 .csv,这两个文件来自另一个脚本,它包含有关连接到网络的设备的信息,这个是导致错误时文件的外观。
文件1.dat:
SN: FCQ1632Y0UQ
Estadio_Admon
ip_address: 148.000.000.123
Run Code Online (Sandbox Code Playgroud)
file2.dat:
Device ID: ESTADIO_19
IP address: 148.000.000.119
Interface: FastEthernet0/3
Port ID (outgoing port): GigabitEthernet0
Device ID: ESTADIO_18
IP address: 148.000.000.118
Interface: FastEthernet0/4
Port ID (outgoing port): GigabitEthernet0
Device ID: ESTADIO_16
IP address: 148.000.000.116
Interface: FastEthernet0/6
Port ID (outgoing port): GigabitEthernet0
Device ID: ESTADIO_PALCOS
IP address: 148.000.000.66
Interface: GigabitEthernet0/2
Port ID (outgoing port): GigabitEthernet0/1
SN: FCQ1632Y0US
Device ID: ESTADIO_22
IP address: 148.000.000.122
Interface: FastEthernet0/8
Port ID (outgoing port): GigabitEthernet0
Device ID: SIPCCEF485DE89A
IP …Run Code Online (Sandbox Code Playgroud) 我在 .sh 中使用 for 循环时出现“接近意外标记‘do’的语法错误”错误。这是代码:
#!/bin/sh
# some code
for flux in $(ls -d /home/eai/*/*/*) ; do
FICHIER=$(ls -p -tr $flux | grep -v / | head -n 1)
if [[ $FICHIER ]] ; then
# some code
fi
done
Run Code Online (Sandbox Code Playgroud)
1) sh script.sh 或 bash script.sh
'cript_1409.sh: line 24: syntax error near unexpected token `do
'cript_1409.sh: line 24: `for flux in $(ls -d /home/eai/*/*/*); do
Run Code Online (Sandbox Code Playgroud)
2) ./script.sh
-bash: ./script_1409.sh: /bin/sh^M: bad interpreter: No such file or directory
Run Code Online (Sandbox Code Playgroud)
1) …
我使用 Ubuntu 服务器 16.04 (xenial) 并希望按照给定的系统启动次数执行一次命令,然后自动将其删除。解决方案似乎包括两个阶段:
将命令添加到/etc/bash.bashrc.
以某种方式使其在 x 次bash.bashrc执行后被删除。
我已经通过在bash.bashrc以下命令的末尾添加来完成解决方案的第 1 阶段:
echo "Welcome!"
Run Code Online (Sandbox Code Playgroud)
有没有办法在 Bash 中这样做?
cat <<-"EOF" > /opt/bootmsg.sh
#!/bin/bash
echo welcome!
count='0'
((count++))
sed -i -e "s/^count='[01234]'$/count='${count}'/" "$0"
if ((count>=5)); then rm -- "$0"; fi
EOF
chmod +x /opt/bootmsg.sh
# Add in the end of bash.bashrc:
# [ -f /opt/bootmsg.sh ] && /opt/bootmsg.sh
Run Code Online (Sandbox Code Playgroud)
如果您发现代码有什么问题,请发布包含该代码的固定版本的答案以及我做错了什么的解释。
建议使用 shell 脚本使用 ASCII 值查找字符串中大写和小写字母的数量。
我需要一个 shell 脚本,它将一些字符串添加到目录和子目录中存在的文件名(.c 文件)中。
例如:如果lokesh是父目录并且在它里面lokesh1,lokesh2是子目录和里面lokesh1( 1.c, 2.c...) 和里面lokesh2( a.c, b.c...)。因此,在运行脚本后,我想将其更改为 ( x_1.c, x_2.c... ) 和(x_a.c, x_b.c...)。
我有一个文本文件;其内容如下。
$ cat file.txt
[] [1]foo1 bar1
[] [2]foo2 bar2
[] [35]foo3 bar3
[] [445]foo4 bar4
[] [87898]foo5 bar5
Run Code Online (Sandbox Code Playgroud)
我可以使用 awk 成功删除第一列,但我无法删除 [num] 个字符,因为它与字符串关联。
我正在尝试获得如下输出
$ cat file.txt
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5
Run Code Online (Sandbox Code Playgroud) 如何在 Bash 脚本中测试 Nginx 配置文件?目前我在 shell 中使用 -t :
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Run Code Online (Sandbox Code Playgroud)
但我想在脚本中做到这一点?
我正在尝试使用echo命令转义以下代码,但我一直在获取实际的八位字节而不是表情符号。
另外我在哪里可以找到表情符号的八位字节值?我似乎总能找到UTF-8价值。
#!/usr/bin/env bash
UNICORN='\360\237\246\204\n'
FIRE=''
# this does not work when I run the script
printf '\360\237\246\204\n'
printf "Riding a ${UNICORN:Q}"
echo "Riding a ${UNICORN:Q}" #[Fails]: how to extract the actual emoji?
Run Code Online (Sandbox Code Playgroud)
EDIT_1:阅读评论后更新代码
#!/usr/bin/env bash
# Note: use hexdump -b to get one-bye octal display
UNICORN_UTF8=$'\360\237\246\204'
printf "U1F525\n"|hexdump -b # [ASK]: How to translate the return value to a valid UTF8 ?
FIRE_UTF8=$'\125\061\106\065\062\065\012'
echo "Riding a ${UNICORN_locale_encoding}"
echo "${UNICORN_UTF8} + ${FIRE_UTF8}"
Run Code Online (Sandbox Code Playgroud)
EDIT_2:发布最终代码。它有点工作。
#!/usr/bin/env …Run Code Online (Sandbox Code Playgroud) 我有一个脚本,其中包含
while true
sudo mycmd
sleep 10000
[ ... ] ; break
end
Run Code Online (Sandbox Code Playgroud)
当我在 bash 中运行脚本时,我必须在运行完sudo mycmd.
我记得yes | somecommand可以重复提供yes作为标准输入到somecommand,作为“是或否”重复问题的答案。我想知道如何向脚本重复提供我的密码?
谢谢。