对于shell脚本,该变量是一个表名。
If the table name is test then execute
while IFS=',' read a; do drop.sh $a; done < abc
or else execute
while IFS=',' read a; do create.sh $a; done < abc
我如何在Linux中实现这一点?
我不习惯编写Bash脚本,Google也没有帮助找出这个脚本的错误所在:
#!/bin/bash
while read myline
do
done
echo "Hello"
while read line
do
done
exit 0
我得到的输出是:
./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'
我的bash版本是:
GNU bash, version 3.2.25(1)-release (i686-re
为了逐行处理bash中的文本文件,我通常实现一个while循环,如下所示:
function doSomething() {
local inputFile="$1"
local fd=""
local line=""
exec {fd}<"$inputFile" # open file
echo "Opened ${inputFile} for read using descriptor ${fd}"
while IFS='' read -r
我在WSL上使用的是ubuntu 18。
例如,使用apt,我得到了错误消息sudo apt autoremove:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
dns-root-data dnsmasq-base ebtables libdumbnet1 libllvm9 liblxc-common
在我的项目中,我使用linux shell来获取一些数据。 我在运行脚本时发现了一个错误。 我编写了一个test.sh来测试我的代码: [john@node04 findApp]$ cat test.sh
while read line
do
echo $line
while read Row
do
done < daysnameFiles
done < allUserAppIn 当我运行测试代码时,我得到语法错误 [john@node04 findApp]$ sh test.sh
test.sh: line 6: syntax error n
如果我做下面的事情
tr -cd 'a-z' < /dev/urandom | fold -w10 | head -n40 > pw
for f in $(seq -w 40); do echo linux$f.example.com;done > hosts
while read -r x && read -r y <&3; do ssh $x -l root echo $y;done <hosts 3<pw
然后,循环在第一次迭代之后停止,但是如果我插入了一个echo (这不是我想要的),那么它会遍历所有的40
我需要从linux获得以字节为单位的交换内存大小,因此我执行以下命令:
{ read firstLine; while read f t s u p; do echo $s; done;} < /proc/swaps
它返回:
523260
523260
我要回去:
1046520
我试着用:
{ read firstLine; while read f t s u p; do echo $s++; done;} < /proc/swaps
但是我知道使用$s++是不正确的,所以如果有人有线索或想法,我需要用读取的值来增加$s变量吗?
例如,如果我有:
523260 256523
我有下面的文件夹列表,名称中有两个单词,用空格分隔
WEB TEH
COMPUTER SCIENCE
UNIX LINUX
我会创建一个shell脚本,这个脚本可以浏览目录并从每个文件夹中提取第一个和第二个单词,
并将每一个提取的单词放入文本文件中(每个单词放在一行中)。
file1.txt包含:
WEB
COMPUTER
UNIX
而file2.txt包含:
TECH
SCIENCE
LINUX
谢谢
编辑:
我创建了这个脚本:
#!/bin/bash
# Read folder name and put it in file
read -p "Enter Your Path: &
我正在学习Linux命令,我正在练习并尝试编写一个基本的shell脚本,它使用递归列出了子文件夹中的所有文件和文件,如ls *。
#!/bin/bash
# list-all: one command to list them all!!!!
listit () {
if [ -d "$1" ]
then
listit "$1"
else
echo "$1"
fi
}
ls | while read items
在Fedora Linux shell中键入以下代码时出现问题:
[root@localhost KDVDRam]# apt-get install kdvdramtools-0.4
Reading Package Lists... Done
Building Dependency Tree... Done
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
Aborted (core dumped)
[root@localhost KDVDRam]# sudo apt-get install k
我想问一下linux中的ftp连接,如何使用终端检查linux中的ftp匿名连接?我使用while循环读取vsftpd.conf文件,就像这样
while read line
do
if [ Anonymous_enable=YES ];
then echo " Accept connection"
elif [ Anonymous_enable=NO ];
then echo "Not Accept"
fi
done<vsftpd.conf
我正在寻找一种方法来欺骗Linux程序,使其认为它连接到TTY,以便输出是行缓冲的,而不是块缓冲的。
当我在终端仿真器中直接调用程序时,它的行为是正确的,但是如果我通过管道将它的输出传递给while read line; do ...; done或cat,它就不会。
基本上,我想以这样的方式结束:
exec "${some_strange_wrapper}" "${some_program}" | while read line;
do
# Do something...
done
我想要生成一个排序列表与所有8位数字-从00000000到99999999。我输入了shell:
f() {
while IFS="" read -r line; do
for i in {0..9}; do
echo "$line$i";
done;
done
}
echo | f | f | f | f | f | f | f | f | tee result.txt | wc -l
反应是
bash: echo: write error: Interrupted system call
bash: echo: write
我对Linux脚本非常陌生。我相信这很简单,但我想不出来。作为脚本的一部分,我试图将文件的内容作为脚本中命令的参数传递:
while read i
do $COMMAND $i
done < file.lst
除了文件的第一行之外,我希望传递file.lst的每一行作为命令的参数。我该怎么做?
编辑:
下面是脚本的部分:
while read i
do cp --recursive --preserve=all $i $DIR
done < $DIR/file.lst
GNU bash,版本4.1.5(1)-发布-(i486-pc- Linux -gnu) Ubuntu Linux 10.04
这会将var打印为1234,但不会打印var2
#!/bin/bash
var=1234
while var=9876 var2=765 read line
do
echo $var $var2
echo $line
done <datafile
echo out of loop $var
o/p
1234
This is line 1
1234
This is line 2
1234
This is line 3
1234
This
因此,我试图读取文件的每一行,但它给了我一个错误。
没有这样的文件或目录
但是文件确实存在
以下是代码:
echo the file path is $pathToGo
while read p
do
echo $p
done < $pathToGo
产出:
现在,如果我硬编码路径,它就会运行得很好:
pathToGo="C:\Users\sorel\Bash\CW3\4\files\indexFiles\3346"
echo the file path is $pathToGo
while read p
do
echo $p
done &
我试图把用户输入的所有数字加起来,但是用户可以输入多少个数字是没有限制的。如何在linux脚本中编写此代码?
到目前为止,我有这样的看法:
firstNumber=0
secondNumber=0
number=0
echo Please enter two numbers to add up
read firstNumber
read secondNumber
echo Would you like to keep adding numbers? YES OR NO
read answer
if answer = YES
then
echo Please add another n
我想使用linux脚本读取文件的内容。file_list.txt的内容如下:
abc
def
ghi
读它的脚本是read_file_content.sh
#!/bin/bash
for file in $(cat file_list.txt)
do
"processing "$file
done
当我以./read_file_content.sh的形式运行命令时,我会得到以下错误:
./read_file_content.sh: line 6: processing abc: command not found
./read_file_content.sh: line 6
我试过这个例子:loop = $(($loop + 1))
但是Linux Shell告诉我
./while_and_read.sh:第17行:循环:未找到命令
在Linux中,我应该如何使用循环vairable来增加每循环1次呢?
#!/bin/bash
echo "Please type a number which it should be greeter than 0."
read -p "Number:" -t 5 x
sum=0
loop=0
if [ $x -le 0 ];then
echo "Please type a
我在Linux机器上有这样的脚本:
#!/bin/bash
echo "Creating a user file using while loop"
Name=" "
Surname=" "
for ((i=1;i<6;i++)); do
((a=((i-1)%5)+1))
echo "USER: $i"
echo "_________________"
echo "Enter first name"
read Name
echo "
我不明白为什么当"${ARRAY[@]}"被引用("...")时,它被扩展为多个单词?
举个例子:
IFS=":" read -ra ARRAY <<< "foo:bar:baz"
for e in "${ARRAY[@]}"; do echo $e; done
foo
bar
baz
我在引号中展开的任何其他变量,比如"${VAR}",都会产生一个单词:
VAR="foo bar baz"
for a in "${VAR}"; do echo $a
将脚本运行到ssh并返回iostat错误时,
solaris_command = "iostat -en | awk 'NR>=3 && $4 > 0' | while read a b c d e; do echo $e,$a,$b,$c,$d; done"
for line in server_list:
line_arr = line.split(',')
if line_arr[0] == 'SunOS':
ie = subprocess.Popen(["
我的UbuntuServer18.04系统虽然继续工作,但已经无法应用任何更新。我不知道我所做的任何系统改变都可能是罪魁祸首。
我能做些什么来解决这个问题?我试过自动移除、清除等。
这是典型的输出,但是任何apt命令都会产生类似的错误:
# apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be REMOVED:
linu
所以我在linux终端上写了一个程序,我的程序有两个部分。第一部分是除法,第二部分是一些数字的MOD计算。退出第一部分的方法是将999放在要除以的输入中。
我的问题是,即使用户输入999作为第一个输入,用户也必须输入第二个号码。我想知道这些是否像windows中的东西,您可以在windows中执行goto :在linux中进行其他定位。这是代码:
echo "Enter the number to divide (dividend) (enter 999 to quit):"
read numberOne
[IF NUMBERONE = 999, JUMP TO SECONDP
我已经编写了shell脚本,它从输入文件中读取命令并执行命令。我有这样的命令:
cat linux_unit_test_commands | grep "dmesg"
在输入文件中。在执行shell脚本时,我收到下面的错误消息:
cat: |: No such file or directory
cat: grep: No such file or directory
cat: "dmesg": No such file or directory
剧本:
#!/bin/bash
while read line
do
output=`$line
在Ubuntu和其他形式的Linux上工作,但不能在Mac OS X上工作的用法示例。
while true; do
read -e -n 1 -p "Do you like Pie? > " -i y ANSWER
case $ANSWER in
[Nn]* ) exit;;
[Yy]* ) `./calculate-pi.sh`; break;;
* ) echo "Please answer y or n.";;
esac
done
while read line
do
read -p "Are you alright? (y/n) " RESP
if [ "$RESP" = "y" ]; then
echo "Here i want to do something, but prompt does not wait for the answer"
else
echo "You need more bash programming"
fi
done < find-spam-send-php-ver2.log
==>此脚本
每当我执行输出多行的linux命令时,我都想对输出的每一行执行一些操作。一般来说,我是这样做的
command something | while read a
do
some operation on $a;
done
这可以很好地工作。但我的问题是,有没有一些方法可以通过预定义的符号(不知道如何调用它)来访问每一行?/像$这样的东西?或者..。$!..或者..。$_
有没有可能做
cat to_be_removed.txt | rm -f $LINE
bash中是否有预定义的$LINE ..或者前一条是最短的路。即。
cat to_be_removed.txt | while read
我试图用bash脚本编写一个简单的Linux make命令。以下是我到目前为止所写的:
#!/usr/bin/env bash
function make_cmd()
{
read target colon sources
for src in $sources; do
if [ $src -nt $target ]; then
while read cmd && [[ $(echo "$cmd" | grep "$(printf '\t')"*) ]]; do
echo "e
我正在linux上学习bash,我找到了用于“同时循环从文件中读取”的脚本,并做了如下修改,我正在尝试读取包含所有git repos的列表文件,并且我想迭代所有这些git repos并为它们运行git status:
while read line;
do echo $line;
cd $line;
git status;
done < repo.list
这个部分工作正常,我可以在stdout中看到每个repo的状态,但是当我尝试将输出写到另一个文件时,它不起作用:
while read line;
do echo $line;