在一个Bash脚本中,我想将一行分成几块,并把它们放入一个数组中。
该行:
Paris, France, Europe
我想让他们像这样的数组:
array[0] = Paris
array[1] = France
array[2] = Europe
我想用简单的代码,命令的速度并不重要。我该怎么做?
我正试图为文件中列出的用户创建一个登录程序。当我试图将名称存储到和数组中并检查它们是否存在时,我只返回标头。所以如果我把名字作为fname,它说它是存在的。但是如果我把Bob作为fname,它说它没有。我如何将每个名字和姓氏存储到一个数组中?
users.txt
NAME PASSWORD DEPT AUTH
Bob Jones 12345678 MKTG N
Sam Smith password MKTG Y
Pat Johnson 87654321 SALES
为什么最后两个数组元素被截断?
我正在尝试从bash中的字符串生成一个数组,使用\n作为分层字符。使用命令替换和tr,我接近了我想要的,现在我得到了奇怪的输出。
下面的脚本文件
输入字符串:abcd\nefgh\nijkl\nmnop
#!/bin/bash
oper () {
local fct=$1
local s=$2
IFS='\n' read -ra sArr <<< "$s"
for item in ${sArr[@]}
do
#!/usr/bin/bash
dirs="$(ls /)"
IFS=' ' read -a dir <<<"$dirs"
for item in "${dir[@]}"; do
if [[ $item != "proc" ]]; then
du -sh /$item
fi
done
我想列出/中的文件夹,并检查它们的大小,ls / response bin boot dev etc home lib lib64 media mnt opt proc root r
因此,我对这个shell脚本以及如何实现我想要的东西有一些问题。我正在从一个文件中读入,并使用以下命令进行拼写检查:
cat $1|aspell list
这将列出拼写错误的单词,但我如何将其分隔,以便获得如下输出:
'agle' is mispelled. Press "Enter" to keep
this spelling, or type a correction here: angle
'objekt' is mispelled. Press "Enter" to keep
this spelling, or typ
我对bash脚本和写bash脚本都很陌生。我已经声明了一个数组,并给出了值。但是,当我在循环中传递数组时,值不会进入循环中。下面是我的代码
declare -a files=( "A1S0" "D1S0" "D2S0" "D3S0" "D4S0" "D5S0" "D6S0" )
command_to_get_filesizes() {
for i in "${!files[@]}"; do
echo Processing file "${i[${
假设我想要创建一个提取所有作为参数列出的tar.gz和tar.bz2文件的脚本。到目前为止,我所做的是:
if [[ $@ =~ .*"tar.gz".* ]] || [[ $@ =~ .*"tar.bz2".* ]]; then
for i in $@ =~ .*"tar.gz".*; do
tar -xvzf "$i"
done
for p in $@ =~ .*"tar.bz2".*; do
tar -xvjf "$p"
done
else
echo "tar.gz
我想知道在Bash中是否有一种有效的方法来检查元素是否存在于数组中?我正在寻找一些类似于我可以在Python中做的事情,比如:
arr = ['a','b','c','d']
if 'd' in arr:
do your thing
else:
do something
我见过使用关联数组的bash for Bash 4+的解决方案,但我想知道是否还有其他解决方案。
请理解,我知道简单的解决方案是在数组中迭代,但我不想这样。
我想在bash中迭代几个列表。现在我有
array=("list1item1 list1item2" "list2item list2item2")
for list in "${array[@]}"
do
for item in $list
do
echo $item
done
done
这不管用。有没有办法在bash中创建列表列表、数组数组或列表数组?
我想遍历list1,然后遍历list1中的列表标题。然后迭代list2和list2中的列表项。
文件名:Tree(可执行文件)
#!/bin/bash
for i in $*
do
if [ -d $i ]; then
echo "===================$i================" >> ?????
tree -L 1 $i >> ?????
fi
done
正如您所看到的,我想列出我输入的参数的树结构,我想让所有的树结构重定向到最后一个文件( id是$#),它是“?”在这个脚本里,因为我不知道怎么写。
例如:
./Tree ./* README
是的,所有目录树结构都
在bash脚本中,我希望从变量中删除两个项。具体来说,在OS上,我列出了/Users目录的内容,并希望变量不包含".localized“或”共享“。这是密码..。
# List the system users and load them into a variable
systemUsers=`ls /Users/`
此变量将列出所有用户主目录。如何确保变量不包含上述两项?
要访问数组元素,@和$工作。例如:
@item_price_list = (5 , 8 , 24);
@item_name_list = ("Apple", "Banana", "Mushroom");
print "The price of one @item_name_list[1] is @item_price_list[1] gold coins.\n";
print "The price of one $item_name_list[1] is $item_price_list[1] gold coins.\n&